且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建数据库连接的***方法

更新时间:2023-01-01 11:05:48

如果您的应用程序适用于多个连接,则首选使用连接池。它已经在Java中实现,您可以轻松使用它。
这个在使用tomcat的web应用程序中使用池的例子(如果你编写web-app你也可以使用tomcat的池,它会更好)

If your app will works with many connections it's preferred to use pool of connections. It's already realized in Java and you may easily use it. This example of using pool in web app that uses tomcat(if you writing web-app you also can use tomcat's pool, and it would be better)

package usepool;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

/**
*
* @author brainless
*/
public class ConnectionPool {

private static DataSource datasource;
public static String dbURL = "jdbc:mysql://localhost:3306/"
                + "<YourDataBase>?useUnicode=true&useEncoding=true&characterEncoding=UTF-8";
public static String driverClass = "com.mysql.jdbc.Driver";
public static String userName = "root";
public static String password = "password";
public static boolean jmx = true;
public static boolean testIdle = false;
public static boolean testBorrow = true;
public static boolean testReturn = false;
public static int validationInterval = 30000;
public static int timeBetweenEviction = 30000;
public static int maxActive = 100;
public static int initialSize = 10;
public static int maxWait = 10000;
public static int removeAbandonedTimeout = 60;
public static int minEvictableIdle = 30000;
public static int minIdle = 10;
public static boolean logAbandoned = true;
public static boolean removeAbandoned = true;
public static String jdbcInterceptors = "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
        + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer";

private ConnectionPool() {
}

public static synchronized DataSource getInstance() {
    if (datasource == null) {
        PoolProperties p = new PoolProperties();
        p.setUrl(dbURL);
        p.setDriverClassName(driverClass);
        p.setUsername(userName);
        p.setPassword(password);
        p.setJmxEnabled(jmx);
        p.setTestWhileIdle(testIdle);
        p.setTestOnBorrow(testBorrow);
        p.setTestOnReturn(testReturn);
        p.setValidationInterval(validationInterval);
        p.setTimeBetweenEvictionRunsMillis(timeBetweenEviction);
        p.setMaxActive(maxActive);
        p.setInitialSize(initialSize);
        p.setMaxWait(maxWait);
        p.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        p.setMinEvictableIdleTimeMillis(minEvictableIdle);
        p.setMinIdle(minIdle);
        p.setLogAbandoned(logAbandoned);
        p.setRemoveAbandoned(removeAbandoned);
        p.setJdbcInterceptors(jdbcInterceptors);
        datasource = new DataSource();
        datasource.setPoolProperties(p);
    }
    return datasource;
}

public static synchronized void closePool() {
    if (datasource != null) {
        datasource.close();
    }
}

}

这个类是单身。要在代码中建立连接,您需要使用类似的东西

This class is singleton. To get connection in your code you need to use something like this

import usepool.ConnectionPool;
/* 
* code
*/
connect = ConnectionPool.getInstance().getConnection();