DBCP
DBCP是Apache推出的數(shù)據(jù)庫(kù)連接池(Database Connection Pool)蝶押。
操作步驟:添加jar包:
commons-dbcp-1.4.jar
commons-pool-1.5.6.jar添加屬性資源文件
dbcpconfig.properties文件囤官。
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_user
username=root
password=root
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=REPEATABLE_READ
- 編寫(xiě)數(shù)據(jù)源工具類(lèi)
public class DBUtil {
public static Connection getConnection(){
Connection conn = null;
try {
conn = getDataSource().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
private static DataSource getDataSource(){
DataSource dataSource=null;
Properties p = new Properties();
try {
p.load(DBUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
dataSource = BasicDataSourceFactory.createDataSource(p);
} catch (Exception e) {
throw new RuntimeException("獲取DataSource對(duì)象失敗");
}
return dataSource;
}
}
C3P0
操作步驟:
- 添加jar包
c3p0-0.9.1.2.jar
- 編寫(xiě)配置文件
創(chuàng)建一個(gè)c3p0-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_user</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
</c3p0-config>
- 編寫(xiě)數(shù)據(jù)源工具類(lèi)
public class DBUtil {
private static DataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection(){
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
JavaWeb之Tomcat管理數(shù)據(jù)源
上面2中方式都需要導(dǎo)入jar包扰魂,在JavaWeb服務(wù)器Tomcat中其實(shí)內(nèi)置了數(shù)據(jù)源蛇捌。所以不需要導(dǎo)入jar包恕洲。
Tomcat內(nèi)置數(shù)據(jù)源其實(shí)也是DBCP靴迫,是Tomcat的lib目錄下的tomcat-dbcp.jar栋猖。
配置數(shù)據(jù)源的步驟:
拷貝數(shù)據(jù)庫(kù)連接的jar mysql-connector-java-5.1.7-bin.jar到tomcat/lib目錄下
配置數(shù)據(jù)源XML文件
<Context>
<Resource name="jdbc/login_register" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db_user"/>
</Context>
如果是在當(dāng)前應(yīng)用的META-INF中創(chuàng)建context.xml蕴潦,編寫(xiě)數(shù)據(jù)源像啼,那么只有當(dāng)前應(yīng)用可以使用。
使用連接池潭苞,封裝工具類(lèi)
public class DBUtil {
public static Connection getConnection(){
Connection conn = null;
try {
Context c = new InitialContext();
DataSource dataSource = (DataSource) c.lookup("java:/comp/env/jdbc/login_register");//這里的jdbc/login_register和篇配置文件中的name屬性一致
conn = dataSource.getConnection();
return conn;
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
return conn;
}
}