【问题标题】:How can I use one database connection object in whole application? [duplicate]如何在整个应用程序中使用一个数据库连接对象? [复制]
【发布时间】:2014-01-07 03:12:44
【问题描述】:

我创建了这个返回连接对象的类。我用过 MySQL 数据库。

public class Connect_db {        
    public Connection getConnection(String db_name,String user_name,String password)
    {
        Connection con=null;
        try
        {
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost/"+db_name+"?user="+user_name+"&password="+password);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return con;        
    }
}  

现在我要做的就是将此类实例化一次并获取连接对象。 我想在整个应用程序中使用同一个对象。 另一个解决方案也将不胜感激。

【问题讨论】:

标签: java mysql jakarta-ee jdbc odbc


【解决方案1】:

我真的很喜欢 Lashane 的回复,我使用代码创建了一个DataSource 解决方案。我还重新设计了它只存储DataSource 而不是Connection,以防您确实想打开多个。

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class SignalDB {

    private static MysqlDataSource ds = null;

    public static MysqlDataSource getDataSource(String db_name) {
        if (ds == null) {
            // db variables set here
            getDataSource(db_url, db_user, db_password, db_port);
        }
        ds.setDatabaseName(db_name);
        return ds;
    }

    private static void getDataSource(String db_url, String db_user, String db_password, int db_port) {
        try {
            ds = new MysqlDataSource();
            ds.setServerName(db_url);
            ds.setUser(db_user);
            ds.setPassword(db_password);
            ds.setPort(db_port);
        } catch (Exception e) {
            System.out.println("MysqlDataSource err: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

然后您可以使用以下方法创建连接:

con = SignalDB.getDataSource("database_name").getConnection();

我添加了每次连接到不同数据库的功能,在某些情况下,就像我们一样,这是您需要即时执行的操作。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    很原始的方式,可以通过

    得到一个Connection实例

    Connect_db.getConnection(dbName,userName,passwd);

    在任何类中,因为它是静态方法。

    public class Connect_db {   
    static {
         try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("MySQL db driver isnot on classpath");
        }
    }
    public static Connection getConnection(String db_name,String user_name,String password) throws SQLException
    {
        return DriverManager.getConnection("jdbc:mysql://localhost/"+db_name+"?user="+user_name+"&password="+password);    
    }
    

    }

    如果您的应用程序是多线程的并且应该很好地使用池

    【讨论】:

      【解决方案3】:

      我想你需要单例模式,这里是一个简单的例子:

      public class Connect_db {        
          static Connection con=null;
          public static Connection getConnection()
          {
              if (con != null) return con;
              // get db, user, pass from settings file
              return getConnection(db, user, pass);
          }
      
          private static Connection getConnection(String db_name,String user_name,String password)
          {
              try
              {
                  Class.forName("com.mysql.jdbc.Driver");
                  con=DriverManager.getConnection("jdbc:mysql://localhost/"+db_name+"?user="+user_name+"&password="+password);
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
      
              return con;        
          }
      } 
      

      那么你就可以像这样使用连接了:

      Connect_db.getConnection().somemethods();
      

      但是,您应该想一想 - 当多个线程尝试向数据库发出请求时,这将如何在多线程环境中工作。

      【讨论】:

      • @JosefN 我可以想象几个单线程应用程序可以将单例用于数据库,例如某种长时间运行的作业,它操作数据库并且需要轻松访问来自不同类的连接
      • 好的,对不起,我在不同的应用程序上工作,要清理我的评论。谢谢
      猜你喜欢
      • 2019-03-06
      • 2019-10-06
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多