FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   jdbc in loop method
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: jdbc in loop method  (Read 714 times)
bshevade


jdbc in loop method
« on: Dec 25th, 2003, 2:59am »

hi,
 
i am trying to connect to a database and select data from it, using jdbc. The jdbc code to do this is shown below. it works from setup() but not from loop() and in my loop i connect and select only when the c variable is true. This variable is set to false as soon as i enter the get_Connection method and is again set to true when i leave the get_Connection method. but this code doesnt work? so is it a problem with jdbc in loop method or am i missing something.
 
int c = 1;
 
void loop(){
  if (c==1)
  {
    c =0;
    conn = get_Connection();
 
    System.out.println("conn in loop");
  }
}
 
private java.sql.Connection get_Connection(){
  try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    String url = "jdbcdbc:wordnet2";
    dbconn = java.sql.DriverManager.getConnection(url,"","");
    System.out.println("After getting connection");
 
     
 stmt = dbconn.createStatement();
 String query="select * from word where lemma='entity'";
 System.out.println("query is "+query);
 java.sql.ResultSet rs = stmt.executeQuery(query);
 
 while(rs.next()){
 String gid = rs.getString("wordno");
   System.out.println("wordno "+gid);
 }//end of while(rs.next())
 rs.close();
 
  }catch(Exception e){
    e.printStackTrace();
  }
  System.out.println("before returning dbconn "+dbconn);
  c = 1;
  return dbconn;
}
 
thanks,
-bageshree.
 
fry


WWW
Re: jdbc in loop method
« Reply #1 on: Dec 25th, 2003, 7:12pm »

most of that should go into the setup() section. you'll want to create the connection object (and probably create the statement object too) inside setup(). since loop() gets called many times per second, you'll then use the statement object inside loop() to run your query.
 
however, chances are you problem don't want to keep banging on the database to do a query 10-30 times a second, so you'll want to do something else to queue up running the query when you actually want to (after actual changes/user interaction/etc)
 
Pages: 1 

« Previous topic | Next topic »