【问题标题】:Processing OOP connecting to MySQL database处理连接到 MySQL 数据库的 OOP
【发布时间】:2026-01-15 20:00:01
【问题描述】:

我和一个朋友正在尝试用 Processing 编写一个程序。程序需要能够随机连接到我们的 MySQL 数据库拉取信息并显示出来。我们已经完成了这么多工作。使用以下代码

    import de.bezier.data.sql.*;

     MySQL dbconnection;

     void setup()
     {
      size( 100, 100 );

     String user     = "username";
     String pass     = "password";

     // name of the database to use
     String database = "databasename";

     // name of the table that will be created
     //
     String table    = "tablename";

     //
     dbconnection = new MySQL( this, "ip", database, user, pass );

     if ( dbconnection.connect() )
     {
    // now read it back out
    //
       dbconnection.query( "SELECT COUNT(id) FROM quiz_table" );
       dbconnection.next();
       int NumberOfRows = dbconnection.getInt(1);
       float random = random(1, NumberOfRows);
       int roundrandom = round(random);
       println(" Row Number:  " + roundrandom );


       dbconnection.query( "SELECT * FROM quiz_table WHERE id =" + roundrandom);

       while (dbconnection.next())
       {
        int n = dbconnection.getInt("id");
        String a = dbconnection.getString("name");
        String c = dbconnection.getString("charactor");
        String m = dbconnection.getString("game");
        int y = dbconnection.getInt("year");
        String q= dbconnection.getString("quote");
        println(n + "   " + a + "   " + c + "   " + m + "   " + y + "   " + q);
       }
        }
        else
        {
         // connection failed !
         }

         }

          void draw()
              {
                   // i know this is not really a visual sketch ...
              }

这似乎工作正常。但是我们计划让程序执行更多任务并让事情更易于管理我们想要制作一些对象在这种情况下我想要制作一个在调用时将连接到数据库的对象。以下是我想出的,但尽管重新设计了几种方法,我还是无法让它发挥作用。

    import de.bezier.data.sql.*;

    MySQL dbconnection;
    connect1 myCon;

    void setup()
              {
             size(300,300);

   myCon = new connect1("username","password","database","table");
   myCon.dbconnect();
              }

      void draw()
        {

        }


      class connect1 {

      String user; 
      String pass; 
      String data; 
      String table; 


     connect1(String tempuser, String temppass, String tempdata, String temptable) {

     user = tempuser;
     pass = temppass;
     data = tempdata;
     table = temptable;

      } 

     void dbconnect(){

 dbconnection = new MySQL( this, "ip", data, user, pass );

if ( dbconnection.connect() )
{
    // now read it back out

    dbconnection.query( "SELECT COUNT(id) FROM table" );
    dbconnection.next();
    int NumberOfRows = dbconnection.getInt(1);
    float random = random(1, NumberOfRows);
    int roundrandom = round(random);
    println(" Row Number:  " + roundrandom );


    dbconnection.query( "SELECT * FROM table WHERE id =" + roundrandom);

    while (dbconnection.next())
    {
        int n = dbconnection.getInt("id");
        String a = dbconnection.getString("name");
        String c = dbconnection.getString("charactor");
        String m = dbconnection.getString("game");
        int y = dbconnection.getInt("year");
        String q= dbconnection.getString("quote");
        println(n + "   " + a + "   " + c + "   " + m + "   " + y + "   " + q);
       }
}
else
{
    println("fail");
}

 }
   //end of class  
  }

对不起,如果这很难理解

【问题讨论】:

    标签: mysql database oop object processing


    【解决方案1】:

    MySQL 的构造函数需要一个 PApplet 作为第一个参数。当您在对象内部调用new MySQL(this 时,this 不再像在您的第一个程序中那样引用主 PApplet。

    解决此问题的最简单方法可能是:

    myCon.dbconnect(this); // send the PApplet as argument
    ...
    void dbconnect(PApplet parent) {
      dbconnection = new MySQL( parent, "ip", data, user, pass );
      ...
    

    另一种选择是将 PApplet 传递给对象的构造函数,将其存储在一个属性中,并在调用 new MySQL 时使用该属性。

    【讨论】:

      最近更新 更多