sql - Too Many Client Task Exception in Java JDBC -
i creating jdbc odbc connection through java programs. have make connection many times . after times program throws many client task exception . how can resolve problem . pasting sample example of requirement
class connectiondemo { public void connect() { try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); connection con=drivermanager.getconnection("jdbc:odbc:dsn"); statement st= con.createstatement(); } catch(exception ex) { ex.printstacktrace(); } } } calling programs class { public static void main(string args[]) { //supose have call connect methods thousands of times solution connectdemo demo= new connectdemo(); dem0.connect(); } }
the key saving connections.why not use static method call connections in:
public class connector { private static final string url = "jdbc:mysql://localhost/"; private static final string login = "root"; private static final string password = "azerty"; private static final string dbname = "videotheque"; private static connector connector; private static connection connection; private connector() { } public synchronized static connector getinstance() { if (connector == null) { connector = new connector(); } return connector; } public static connection getconnection() { if (connection == null) { connection c = null; try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } try { c = drivermanager.getconnection(url + dbname, login, password); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return c; } return connection; }
for connecting can use as:
connector.getinstance().getconnection()
Comments
Post a Comment