首页 > 技术文章 > JDBCUtils,一个操作关系型数据库的小工具

sovagxa 2017-07-31 08:37 原文

先贴代码

 

 1 public class SqlC3p0Utils {
 2     private static ComboPooledDataSource dataSource;
 3     static{
 4         ResourceBundle bundle=ResourceBundle.getBundle("db");//获得配置文件对象
 5         dataSource = new ComboPooledDataSource();//获得连接池对象
 6         //根据配置文件队连接池进行配置
 7         try {
 8             dataSource.setDriverClass(bundle.getString("driverClass"));
 9         } catch (PropertyVetoException e) {
10             // TODO Auto-generated catch block
11             e.printStackTrace();
12             System.out.print("未找到驱动");
13         }
14         dataSource.setJdbcUrl(bundle.getString("url"));  
15         dataSource.setUser(bundle.getString("username"));  
16         dataSource.setPassword(bundle.getString("password"));  
17         dataSource.setMinPoolSize(10);//连接池最小连接数
18         dataSource.setInitialPoolSize(20);//连接池创建时连接数
19         dataSource.setMaxPoolSize(80);//连接池最大连接数
20         dataSource.setAcquireIncrement(10);//链接用完时创建的连接数
21         dataSource.setNumHelperThreads(5);//多线程执行,提升性能
22     }
23     /**
24      * 
25      * @return 返回一个链接
26      */
27     public static Connection getConnection(){
28         try {
29             return dataSource.getConnection();
30         } catch (SQLException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33             System.out.println("未返回链接");
34             return null;
35         }
36     }
37     
38     /**
39      * @see 关闭资源
40      * @param rs
41      * @param stm
42      * @param conn
43      */
44     public static void  reLeast(ResultSet rs,Statement stm,Connection conn) {
45         if(rs!=null){  
46             try {  
47                 rs.close();  
48             } catch (SQLException e) {  
49                   
50                 e.printStackTrace();  
51             }finally{  
52                 rs=null;  
53             }  
54         }  
55           
56         if(stm!=null){  
57             try {  
58                 stm.close();  
59             } catch (SQLException e) {  
60                   
61                 e.printStackTrace();  
62             }finally{  
63                 stm=null;  
64             }  
65         }  
66         if(conn!=null){  
67             try {  
68                 conn.close(); 
69                 System.out.println("conn yiguanbi");
70             } catch (SQLException e) {  
71                   
72                 e.printStackTrace();  
73             }finally{  
74                 conn=null;  
75             }  
76         }             
77     }
78 }

 

该段程序首先执行静态代码块加载配置文件

获得配置文件对象

ResourceBundle bundle=ResourceBundle.getBundle("db");//获得配置文件对象

 

声明连接池对象(静态代码块外)

private static ComboPooledDataSource dataSource;

 

获得连接池对象

dataSource = new ComboPooledDataSource();//获得连接池对象

 

获得配置文件内容

bundle.getString("url")

 

对连接池进行设置

1         dataSource.setJdbcUrl(bundle.getString("url"));  
2         dataSource.setUser(bundle.getString("username"));  
3         dataSource.setPassword(bundle.getString("password"));  
4         dataSource.setMinPoolSize(10);//连接池最小连接数
5         dataSource.setInitialPoolSize(20);//连接池创建时连接数
6         dataSource.setMaxPoolSize(80);//连接池最大连接数
7         dataSource.setAcquireIncrement(10);//链接用完时创建的连接数
8         dataSource.setNumHelperThreads(5);//多线程执行,提升性能

 

以上静态代码块在加载类的时候就执行,并且只执行一次,避免了每次获得对象都要创建连接池的错误,防止了链接堆积过多的问题


 

获得链接:

 1     /**
 2      * 
 3      * @return 返回一个链接
 4      */
 5     public static Connection getConnection(){
 6         try {
 7             return dataSource.getConnection();
 8         } catch (SQLException e) {
 9             // TODO Auto-generated catch block
10             e.printStackTrace();
11             System.out.println("未返回链接");
12             return null;
13         }
14     }

 

关闭资源,放回链接:

 1     /**
 2      * @see 关闭资源
 3      * @param rs
 4      * @param stm
 5      * @param conn
 6      */
 7     public static void  reLeast(ResultSet rs,Statement stm,Connection conn) {
 8         if(rs!=null){  
 9             try {  
10                 rs.close();  
11             } catch (SQLException e) {  
12                   
13                 e.printStackTrace();  
14             }finally{  
15                 rs=null;  
16             }  
17         }  
18           
19         if(stm!=null){  
20             try {  
21                 stm.close();  
22             } catch (SQLException e) {  
23                   
24                 e.printStackTrace();  
25             }finally{  
26                 stm=null;  
27             }  
28         }  
29           
30         if(conn!=null){  
31             try {  
32                 conn.close(); 
33                 System.out.println("conn yiguanbi");
34             } catch (SQLException e) {  
35                   
36                 e.printStackTrace();  
37             }finally{  
38                 conn=null;  
39             }  
40         }             
41     }
42     

 

推荐阅读