Java Swing Timer with SQL -


i'm trying setup thread loops every 100ms every iteration querying table in sql database. here have in public static void main class. how can define connection outside of listener , call query in loop?

 // database credentials final string url = "jdbc:mysql://192.168.0.0/"; final string db = "db"; final string driver = "com.mysql.jdbc.driver"; final string table = "table"; public final connection conn = null; // define listner actionlistener taskperformer = new actionlistener() { public void actionperformed(actionevent evt) { //...perform task... system.out.println("reading info."); try { class.forname(driver); try { conn = drivermanager.getconnection(url+db,"root","pass"); statement st = (statement) conn.createstatement(); string sql = ""; st.executeupdate(sql); conn.close(); } catch (sqlexception s) { s.printstacktrace(); joptionpane.showmessagedialog(null, "error: please try again!"); } } catch (classnotfoundexception cnfe){ joptionpane.showmessagedialog(null, "error:"); } } }; timer timer = new timer( 100 , taskperformer); timer.setrepeats(true); timer.start(); thread.sleep(100); 

}

right it's giving me following error:

exception in thread "awt-eventqueue-0" java.lang.error: unresolved compilation problem: final local variable conn cannot assigned, since defined in enclosing type

you don't want to. connections not thread safe per jdbc specification. hence there no reason or justification connection variable not being local thread. why isn't it?


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -