2

a friend and I are trying to write a program in Processing. The program needs to be able to connect to our MySQL database pull information at random and display it. we have gotten that much to work. with the following code

    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 ...
              }

this seems to work fine. however we plan to make the program preform many more tasks and to keep things more manageable we wanted to make somethings objects in this case i want to make an object that will connect to the database when its called. The following is what i have come up with but despite reworking several ways I can't quite get it to work.

    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  
  }

Sorry if that is at all hard to understand

1 Answer 1

1

The constructor of MySQL expects a PApplet as the first argument. When you call new MySQL(this inside your object, this does no longer refer to the main PApplet as it did in your first program.

The simplest way to fix this might be:

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

Another option would be to pass the PApplet to the constructor of your object, storing it in a property, and using that property when calling new MySQL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.