码迷,mamicode.com
首页 > 其他好文 > 详细

C3P0工具类

时间:2019-06-07 00:18:20      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:param   use   password   localhost   bsp   hang   code   throw   nts   

 1 import com.mchange.v2.c3p0.ComboPooledDataSource;
 2 import javax.sql.DataSource;
 3 import java.sql.*;
 4 
 5 public class C3P0Utils {
 6     //创建C3P0数据源(连接池)
 7     private static DataSource dataSource = new ComboPooledDataSource();
 8 
 9     /**
10      * 从dataSource(连接池) 获得连接对象
11      */
12     public static Connection getConnection() throws Exception {
13         Connection connection = dataSource.getConnection();
14         return connection;
15     }
16 
17     /**
18      * 获得连接池
19      *
20      */
21     public static DataSource getDataSource(){
22         return dataSource;
23     }
24 
25     /**
26      * 增删改都需要获取连接,关闭连接,只是SQL语句不同
27      * 我们可以使用元数据来自动给
28      * SQL设置参数,合并成一个方法。
29      *
30      * String sql:sql语句
31      * Object[] params:参数数组
32      */
33     public static  int update(String sql,Object[] params){
34         try {
35             Connection conn = getConnection();
36             PreparedStatement ps = conn.prepareStatement(sql);
37             //获取到参数个数
38             ParameterMetaData metaData = ps.getParameterMetaData();
39             int count = metaData.getParameterCount();   //参数的个数
40 
41             for (int x = 0; x < count; x++) {
42                 //将参数赋值给对应的 ? 号
43                 ps.setObject(x + 1,params[x]);
44             }
45             return ps.executeUpdate();
46 
47         } catch (Exception e) {
48             e.printStackTrace();
49         }
50         return -1;
51     }
52 
53 
54     /**
55      * 释放资源
56      * resultSet
57      * statement
58      * connection
59      */
60     public static void release(ResultSet resultSet, Statement statement, Connection connection) {
61         if (resultSet != null) {
62             try {
63                 resultSet.close();
64             } catch (SQLException e) {
65                 e.printStackTrace();
66             }
67         }
68         if (statement != null) {
69             try {
70                 resultSet.close();
71             } catch (SQLException e) {
72                 e.printStackTrace();
73             }
74         }
75         if (connection!= null){
76             try {
77                 connection.close();//看Connection来自哪里, 如果Connection是从连接池里面获得的, close()方法其实是归还; 如果Connection是创建的, 就是销毁
78             } catch (SQLException e) {
79                 e.printStackTrace();
80             }
81         }
83     }
85 }
86 
87 
88 
89 C3P0配置文件(c3p0-config.xm)
90 
91 <c3p0-config>
92     <default-config>
93         <property name="driverClass">com.mysql.jdbc.Driver</property>
94         <property name="jdbcUrl">jdbc:mysql://localhost:3306/XXX(数据库名称)</property>
95         <property name="user">用户名</property>
96         <property name="password">密码</property>
97         <property name="initialPoolSize">5</property>
98     </default-config>
99 </c3p0-config>

 

C3P0工具类

标签:param   use   password   localhost   bsp   hang   code   throw   nts   

原文地址:https://www.cnblogs.com/TanBeauty/p/10987254.html

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