简介:
现在很多WEB服务器(Weblogic, WebSphere, Tomcat)都提供了DataSoruce的实现,即连接池的实现。通常我们把DataSource的实现,按其英文含义称之为数据源,数据源中都包含了数据库连接池的实现。
也有一些开源组织提供了数据源的独立实现:
①DBCP 数据库连接池:
DBCP 是 Apache 软件基金组织下的开源连接池实现,使用DBCP数据源,应用程序应在系统中增加如下两个 jar 文件:
Commons-dbcp.jar:连接池的实现
Commons-pool.jar:连接池实现的依赖库
Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
②C3P0 数据库连接池:
实际应用时不需要编写连接数据库代码,直接从数据源获得数据库的连接。程序员编程时也应尽量使用这些数据源的实现,以提升程序的数据库访问性能。
注:lib中导入c3p0-0.9.2-pre1.jar
实例三(C3P0连接池):
1.在src目录下配置好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/bookstore</property>
<property name="user">root</property>
<property name="password">pass</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
</c3p0-config>
2.在JdbcUtil.java中获得DataSourse数据源及创建Connection对象
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtils {
private static DataSource ds = null;
static{
ds = new ComboPooledDataSource();
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
}
3.数据访问层中的增删改查写法:
public class BooksDaoImpl implements BooksDao {
@Override
public void addBook(Books book) {
try{
QueryRunner qr=new QueryRunner(JdbcUtil_dbcp.getDataSource());
String sql="insert into books(id,name,price,author,description,images,category_id) values(?,?,?,?,?,?,?)";
Object params[]={book.getId(),book.getName(),book.getPrice(),book.getAuthor(),book.getDescription(),book.getImages(),book.getCategory().getId()};
qr.update(sql, params);
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Override
public void deleteBook(String id) throws IdNullException {
Connection conn=null;
try{
if(id==null || id.equals("")){
throw new IdNullException("id不能为空");
}
QueryRunner qr=new QueryRunner();
conn=JdbcUtil_dbcp.getConnection();
conn.setAutoCommit(false);
String sql="delete from orderitem where book_id=?";
qr.update(conn,sql, id);
sql="delete from books where id=?";
qr.update(conn, sql, id);
conn.commit();
}catch(Exception e){
try {
conn.rollback();
conn.commit();
} catch (SQLException e1) {
e1.printStackTrace();
}
throw new RuntimeException(e);
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public void updateBook(Books book) throws IdNullException {
try{
if(book.getId()==null || book.getId().equals("")){
throw new IdNullException("id不能为空");
}
QueryRunner qr=new QueryRunner(JdbcUtil_dbcp.getDataSource());
String sql="update books set name=?,price=?,author=?,description=?,images=?,category_id=? where id=?";
Object params[]={book.getName(),book.getPrice(),book.getAuthor(),book.getDescription(),book.getImages(),book.getCategory().getId(),book.getId()};
qr.update(sql, params);
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Override
public List<Books> findAll() {
try{
QueryRunner qr=new QueryRunner(JdbcUtil_dbcp.getDataSource());
String sql="select * from books";
return qr.query(sql, new BeanListHandler<Books>(Books.class));
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Override
public Books findById(String id) throws IdNullException {
try{
QueryRunner qr=new QueryRunner(JdbcUtil_dbcp.getDataSource());
String sql="select * from books where id=?";
return qr.query(sql, new BeanHandler<Books>(Books.class),id);
}catch(Exception e){
throw new RuntimeException(e);
}
}