I wrote a Java class for running MS Sql queries inside my program. This program make a new connection for each query that should be run. I know that this can increase my latency. Here is the calss code:
import java.sql.*;
public abstract class DatabaseManager {
public static ResultSet executeQuery(String SQL, String dbName)
{
ResultSet rset = null ;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName="+dbName+";user=??;password=??;";
Connection con = DriverManager.getConnection(connectionUrl);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rset = st.executeQuery(SQL);
//st.close();
}
catch (ClassNotFoundException e) {}
catch (SQLException e) {}
return rset;
}
public static void executeUpdate(String SQL, String dbName)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName="+dbName+";user=??;password=??;";
Connection con = DriverManager.getConnection(connectionUrl);
Statement st = con.createStatement();
st.executeUpdate(SQL);
st.close();
con.close();
}
catch (ClassNotFoundException e) {System.out.println(e);}
catch (SQLException e) {System.out.println(e);}
}
}
How can I change in the way that just one connection is created and all my queries route throught that connection? Regards.