dbcp连接MySQL数据库

需要jar包:ddbstoolkit-mysql-1.0.0-bate2.jar

        commons-dbcp-1.4.jar

配置文件:mysql-dbcp.properties

配置文件内容:

driver=com.mysql.jdbc.Driver
url=jdbc:MySQL://localhost:3306/testDatabase  
user=root
pwd=1234
initSize=1
maxSize=1

 1 package connectdatabase.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.util.Properties;
 7 
 8 import org.apache.commons.dbcp.BasicDataSource;
 9 
10 public class TestJDBC {
11     //创建连接池
12     private static BasicDataSource ds;
13     static{
14         Properties p = new Properties();
15         try {
16             //加载配置文件
17             p.load(TestJDBC.class.getClassLoader().getResourceAsStream("mysql-dbcp.properties"));
18             String driver = p.getProperty("driver");
19             String url = p.getProperty("url");
20             String user = p.getProperty("user");
21             String password = p.getProperty("password");
22             String initSize = p.getProperty("initSize");
23             String maxSize = p.getProperty("maxSize");
24             //加载驱动
25             ds = new BasicDataSource();
26             ds.setDriverClassName(driver);
27             ds.setUrl(url);
28             ds.setUsername(user);
29             ds.setPassword(password);
30             ds.setInitialSize(new Integer(initSize));
31             ds.setMaxActive(new Integer(maxSize));
32         } catch (Exception e) {
33             e.printStackTrace();
34             throw new RuntimeException("加载配置文件失败", e);
35         }    
36     }
37     /**
38      * 获取数据库连接
39      * @return Connection
40      * @throws SQLException
41      */
42     
43     public static Connection getConnection()throws SQLException{
44         return ds.getConnection();
45     }
46     /**
47      * 关闭数据库连接
48      * @param connection
49      */
50     public static void closeConnection(Connection connection){
51         if(connection != null){
52             try {
53                 connection.close();
54             } catch (Exception e) {
55                 e.printStackTrace();
56                 throw new RuntimeException("关闭连接失败", e);
57             }
58         }
59     }
60     /**
61      * test
62      * @param args
63      * @throws SQLException
64      */
65     public static void main(String[] args) throws SQLException {
66         Connection connection = TestJDBC.getConnection();
67         String sql = "select * from cn_activity";
68         java.sql.Statement s =  connection.createStatement();
69         ResultSet r = s.executeQuery(sql);
70         while(r.next()){
71             String a = r.getString(1);
72             String b = r.getString(2);
73             String c = r.getString(3);
74             String d = r.getString(4);
75             System.out.println(a+"|"+b+"|"+c+"|"+d);
76         }
77     }    
78 }
View Code

 

转载于:https://www.cnblogs.com/sun-hong/p/7573554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值