我写了一个Java类,用于在我的程序中运行MS Sql查询。此程序为应运行的每个查询建立新连接。我知道这会增加我的延迟。这是calss代码:
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);}
}
}
如何更改仅创建一个连接的方式以及我的所有查询都通过该连接进行路由? 问候。
答案 0 :(得分:4)
我不同意,我不会将数据库连接声明为单例。通常的方法是缓存托管数据库连接池。这里的优点是:
看看这个
http://en.wikipedia.org/wiki/Connection_pool
通常的实施方式是 http://sourceforge.net/projects/c3p0/ http://commons.apache.org/proper/commons-dbcp/
如果您在商业应用程序服务器(例如Websphere或Weblogic)中部署应用程序,它们会为数据库连接池提供开箱即用的支持
答案 1 :(得分:1)
您必须实现Singleton模式。它类似于下面:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private static Connection connection = null;
private static Class driver;
public static void loadDriver() throws ClassNotFoundException{
driver = Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
/**
* Check for some connection
* @return true, eсли установленно; false в противном случае
*/
public static boolean isConnection(){
if (connection != null) return true;
return false;
}
/**
* Return connection
*/
public static synchronized Connection getConnection(String url,
String user, String pass) throws SQLException{
//Create connections if we have't work connection.
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection(url, user, pass); //Next string show use without parameters
//connecction = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;" +
"databaseName="+dbName+";user=??;password=??;";);
connection.setAutoCommit(false);
}
return connection;
}
}
你可以使用它:
try{
DBConnection.loadDriver();
conn = DBConnection.getConnection(dburl, dbuser, dbpass);
//get Prepared statement and Result set. You cam create many anstances from one connections.
PreparedStatement ps = null; ResultSet rs = null;
ps = conn.prepareStatement("Some query");
rs = ps.executeQuery();
} catch (SQLException sqlex) {System.out.println("SQL problem");}
finally{ //You can close all connections
rs.close();
ps.close();
//Close DB connections before terminate code.
conn.close()
}
答案 2 :(得分:0)
您应该实现单例模式以获取类的单个实例,以处理您的所有查询请求。以下是Connect to Database with JDBC, Singleton
的示例