DBCP:
代码体现:
工具类:
public class DBCPUtil {
private static DataSource dataSource;
static{
try {
//读取配置文件
InputStream in = DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
Properties props = new Properties();
props.load(in);
//建立数据源对象
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
throw new ExceptionInInitializerError("初始化数据源失败");
}
}
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("从数据源获取连接失败");
}
}
public static DataSource getDataSource(){
return dataSource;
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}java
配置文件:
文件名:dbcpconfig.properties
#链接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day19
username=root
password=sorrymysql
#<!-- 初始化链接 -->
initialSize=10sql
#最大链接数量
maxActive=50数据库
#<!-- 最大空闲链接 -->
maxIdle=20jsp
#<!-- 最小空闲链接 -->
minIdle=5工具
#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=60000url
#JDBC驱动创建链接时附带的链接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,所以这里不须要包含他们。
connectionProperties=useUnicode=true;characterEncoding=utf8code
#指定由链接池所建立的链接的自动提交(auto-commit)状态。
defaultAutoCommit=trueorm
#driver default 指定由链接池所建立的链接的只读(read-only)状态。
#若是没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=xml
#driver default 指定由链接池所建立的链接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=REPEATABLE_READ
C3P0:(优势:能够同时配置多个数据库):
C3P0Util:
public class C3P0Util {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("从数据源获取连接失败");
}
}
public static DataSource getDataSource(){
return dataSource;
}
}
xml配置文件:
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day19</property>
<property name="user">root</property>
<property name="password">sorry</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="aa">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day20</property>
<property name="user">root</property>
<property name="password">sorry</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</named-config>
</c3p0-config>
利用Tomcat管理数据源
a、拷贝数据库驱动jar包到Tomcat\lib目录下
b.在应用的META-INF目录下创建一个名称为context.xml的配置文件
xml文件:
<Context>
<!-- 配置当前应用的一个资源 -->
<Resource name="jdbc/day19" auth="Container" type="javax.sql.DataSource"
username="root" password="sorry" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/day19" maxActive="8" maxIdle="4" />
</Context>
c、启动Tomcat,数据源就给你建好了
d、在应用中如何获取数据源
在jsp中:
<%
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/day19");
Connection conn = ds.getConnection(); System.out.println(conn); conn.close(); %>特别注意:不要在main方法中获取数据源,获取不到