码迷,mamicode.com
首页 > 数据库 > 详细

JDBC学习笔记(19):标准DataSource接口及开源项目DBCP简单使用

时间:2015-04-05 21:41:26      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

  • DataSource用来取代DriverManager来获取Connection
  • 通过DataSource获得Connection速度很快
  • 通过DataSource获得的Connection都是已经被包裹过的(不是驱动原来的连接),他的close方法已经被修改。
  • 一般DataSource内部会用一个连接池来缓存Connection,这样可以大幅度提高数据库的访问速度
  • 连接池可以理解成一个能够存放Connection的Connection
  • 程序只和DataSource打交道,不会直接访问连接池
 1 package com.xxyh.jdbc.datasource;
 2 import java.io.PrintWriter;
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 import java.sql.SQLFeatureNotSupportedException;
 6 import java.util.logging.Logger;
 7 import javax.sql.DataSource;
 8 public class DataSource3 implements DataSource {
 9     @Override
10     public PrintWriter getLogWriter() throws SQLException {
11         // TODO Auto-generated method stub
12         return null;
13     }
14     @Override
15     public void setLogWriter(PrintWriter out) throws SQLException {
16         // TODO Auto-generated method stub
17     }
18     @Override
19     public void setLoginTimeout(int seconds) throws SQLException {
20         // TODO Auto-generated method stub
21     }
22     @Override
23     public int getLoginTimeout() throws SQLException {
24         // TODO Auto-generated method stub
25         return 0;
26     }
27     @Override
28     public Logger getParentLogger() throws SQLFeatureNotSupportedException {
29         // TODO Auto-generated method stub
30         return null;
31     }
32     @Override
33     public <T> T unwrap(Class<T> iface) throws SQLException {
34         // TODO Auto-generated method stub
35         return null;
36     }
37     @Override
38     public boolean isWrapperFor(Class<?> iface) throws SQLException {
39         // TODO Auto-generated method stub
40         return false;
41     }
42     @Override
43     public Connection getConnection() throws SQLException {
44         // TODO Auto-generated method stub
45         return null;
46     }
47     @Override
48     public Connection getConnection(String username, String password)
49             throws SQLException {
50         // TODO Auto-generated method stub
51         return null;
52     }
53 }

首先导入三个jar包:commons-dbcp.jar、commons-collections.jar、commons-pool.jar

 

配置文件dbcpconfig.properties:

 1 driverClassName=com.mysql.jdbc.Driver
 2 url=jdbc:mysql://localhost:3306/jdbc
 3 username=root
 4 password=1234
 5 initialSize=10
 6 maxActive=50
 7 maxIdle=20
 8 minIdle=5
 9 maxWait=60000
10 connectionProperties=useUnicode=true;characterEncoding=gbk
11 defaultAutoCommit=true
12 defaultReadOnly=
13 defaultTransactionIsolation=READ_UNCOMMITTED

修改JdbcUtils.java:

 1 package com.xxyh.jdbc;
 2 import java.io.InputStream;
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 import java.util.Properties;
 8 import javax.sql.DataSource;
 9 import org.apache.commons.dbcp.BasicDataSourceFactory;
10 public class JdbcUtils {
11     private static DataSource myDataSource = null;
12     
13     private JdbcUtils() {
14     }
15     
16     static {
17         try {
18             // 将注册驱动放在静态代码块中只需执行一次
19             Class.forName("com.mysql.jdbc.Driver");
20 //            myDataSource = new MyDataSource2();
21             Properties prop = new Properties();
22             InputStream is = JdbcUtils.class.getClassLoader()
23                     .getResourceAsStream("dbcpconfig.properties");
24             prop.load(is);
25             myDataSource = BasicDataSourceFactory.createDataSource(prop);
26         } catch(Exception e) {
27             throw new ExceptionInInitializerError(e);
28         } 
29     }
30     
31     public static Connection getConnection() throws SQLException {
32 //        return DriverManager.getConnection(url, user, password);
33         // 当需要连接的时候,不再创建,而是从连接池中获取
34         return myDataSource.getConnection();
35     }
36     
37     public static void close(ResultSet rs, Statement stmt, Connection conn) {
38         try {
39             if (rs != null) {
40                 rs.close();
41             }
42         } catch (SQLException e) {
43             e.printStackTrace();
44         } finally {
45             try {
46                 if (stmt != null) {
47                     stmt.close();
48                 }
49             } catch (SQLException e) {
50                 e.printStackTrace();
51             } finally {
52                 if (conn != null) {
53                     try {
54                         conn.close();
55                         // 将连接放回连接池,而不是关闭连接
56 //                        myDataSource.free(conn);
57                     } catch (Exception e) {
58                         e.printStackTrace();
59                     }
60                 }
61             }
62         }
63     }
64 }

测试:

 1 package com.xxyh.jdbc;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 public class Base {
 8     public static void main(String[] args) throws ClassNotFoundException, SQLException {
 9         //test();
10 //        template();
11         for (int i = 0; i < 5; i++) {
12             Connection conn = JdbcUtils.getConnection();
13             System.out.println(conn.getClass().getName());
14             JdbcUtils.close(null, null, conn);
15         }
16     }
17 }
【运行结果】:
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 

 

JDBC学习笔记(19):标准DataSource接口及开源项目DBCP简单使用

标签:

原文地址:http://www.cnblogs.com/xiaoxiaoyihan/p/4394775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!