Dynamic access to MySQL database in Java -


i looking way how access mysql database more dynamically.

for example, want insert text table. have method called insert() called approximately every half of second , insert stuff.

the code looks this:

public void insert(string string) {         class.forname("com.mysql.jdbc.driver").newinstance();         java.sql.connection conn = java.sql.drivermanager.getconnection(...);         java.sql.statement stat = conn.createstatement();         stat.executeupdate("insert mytable (mytext) values ('"+ string +"')");         conn.close(); } 

but think cause many problems because opening , closing connections often.

so isn't there way open "mysqlstream" database , print data without opening , closing connection again , again?

thanks replies.

create connection object @ beginning of program, , use every query. close @ end of program.

oftentimes 1 connection good, may declare static.

public static java.sql.connection conn; public static void buildconnection() {     class.forname("com.mysql.jdbc.driver").newinstance();     conn = java.sql.drivermanager.getconnection(...); }  public void insert(string string)  {     java.sql.statement stat = conn.createstatement();     stat.executeupdate("insert mytable (mytext) values ('"+ string +"')"); } 

in program, may following:

buildconnection(); ... insert("...."); insert("...."); ... conn.close(); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -