2013-12-18 36 views
0

我编写了一个Java类,用于在我的程序中运行MS Sql查询。这个程序为每个应该运行的查询建立一个新的连接。我知道这会增加我的延迟。这里是CALSS代码:使用JDBC运行的查询

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);} 
    } 
} 

我怎么能在仅创建了一个连接和throught是连接我所有的查询路线的方式改变? 此致敬礼。

+0

使用Singleton模式 – SpringLearner

+1

使连接在你的班上一个单身?真的,你应该考虑一些可以提供借鉴和其他整洁的东西的集合选项(如C3PO)。 –

+0

这可能对你有所帮助http://rdeshapriya.com/a-singleton-java-class-for-mysql-db-connection/ – SpringLearner

回答

0

你应该实现单例模式来获得一个类的单个实例,它处理所有的查询请求。这里是一个例子Connect to Database with JDBC, Singleton

+0

另一种选择是通过Spring自动连接你的JDBCConnection。在这种情况下,您还可以重复使用同一连接... –

4

我不同意,我不会声明一个数据库连接作为一个单身。通常的做法是高速缓存一个托管数据库连接池。这里的优点是:

  • 更对数据库的并发访问
  • 管理支配的数据库资源(如果需要连接池收缩)

看一看这个

http://en.wikipedia.org/wiki/Connection_pool

通常的实施方式是 http://sourceforge.net/projects/c3p0/ http://commons.apache.org/proper/commons-dbcp/

如果部署在商业应用程序服务器,诸如WebSphere或Weblogic的应用程序,配备了外的现成支持数据库连接池

+0

最好的实现是什么? –

+0

这取决于你的环境。根据我的经验,我使用了应用服务器给我的数据库连接池,但是当我有供应商提供的软件时,他们对DBCP非常满意。 –

+0

我发现MS SQL Server支持从内部连接池。你有什么资料我怎么用? –

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() 
} 
+0

非常感谢。 –

+0

@Alin查看更新 –

+2

这只是一个解决方案,如果没有多线程,并且如果连接超时并中断则没有问题。否则:使用连接池并在需要时打开连接,并在完成工作单元时关闭。 –