We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Connecting to MySQL2
Page Index Toggle Pages: 1
Connecting to MySQL2 (Read 875 times)
Connecting to MySQL2
Mar 22nd, 2007, 11:20pm
 
dear all,

I have just started working with processing and am trying to pull data from a MySQL database (which I have a little experience of). I know the database is working (at ‘localhost’) as I am able to add and read from it through PHP.

I have installed the MySQL libraries into processing and am trying the example script provided by Florian Jenett  (http://www.bezier.de/mysql/). I am not able to connect property (I think). I initially tried running on the server using localhost and this was unsuccessful, I have now tried to run it on my machine using the IP address and appropriate permissions. But still no luck. Any help?

Thanks for any help, Aidan

Here is my code (libraries added, just not shown here for brevity)

----------------–

MySQL msql;

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

   // this example assumes that you are running the
   // mysql server locally (on "localhost").
   //
   String user     = "GOTTHISRIGHT";
   String pass     = " GOTTHISRIGHT ";

   // name of the database to use
   String database = "- GOTTHISRIGHT ";

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

   // connect to database of server "localhost"
   msql = new MySQL( "localhost", database, user, pass, this );
   
   if ( msql.connect() )
   {  
       // create a table with an id and a word
       msql.execute( "CREATE TABLE IF NOT EXISTS " + table + " ("+
                         "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"+
                         "word VARCHAR( 255 )"+
                     ")"
                   );
                   
       // fill the table with some data
       //
       msql.execute( "INSERT INTO " + table + " (word) VALUES (\"Lorem\")" );
       msql.execute( "INSERT INTO " + table + " (word) VALUES (\"ipsum\")" );
       msql.execute( "INSERT INTO " + table + " (word) VALUES (\"dolor\")" );

       // now read it back out
       //
       msql.query( "SELECT * FROM " + table );
       
       while (msql.next())
       {
           String s = msql.getString("word");
           int n = msql.getInt("id");
           println(s + "   " + n);
       }
       
       // need to find out how many rows there are in table?
       //
       msql.query( "SELECT COUNT(*) FROM " + table );
       msql.next();
       println( "number of rows: " + msql.getInt(1) );
   }
   else
   {
       // connection failed !
   }
}

void draw()
{
   // i know this is not really a visual sketch ...
}
Re: Connecting to MySQL2
Reply #1 - Mar 23rd, 2007, 7:17am
 
belive me, my code is ok.

are you running this as an applet? if so, have you read the notes about applets?
http://bezier.de/mysql/#NOTES

F
Re: Connecting to MySQL2
Reply #2 - Mar 24th, 2007, 10:02pm
 
hey Florian,

I am sure your code is bang-on, the issue is with me understanding things!

I have been trying to run it locally and as an applet. In both instances the MySQL database is remote. I can connect to the database through other means thanks. I am also able to check the server with

shell>  mysql -h <host> -u <user> -p

and this is successful. When I run pde locally I get this message

java.net.SocketException
MESSAGE: java.net.ConnectException: Operation timed out

any thoughts?

thanks, Rowe
Re: Connecting to MySQL2
Reply #3 - Mar 27th, 2007, 8:33am
 
sorry, i lost track ..

the code says "localhost", i guess that's just a typo and you have the correct IP of your mysql-server in there, right?

the error you get happens inside the driver, which is strange cause it should handle these kinds of errors and return an SQLException ...

can you verify that the exception is happening at connect() ? (comment it out and see what's happening)

F
Page Index Toggle Pages: 1