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

dbcp连接MySQL数据库

时间:2017-09-22 12:07:37      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:except   too   jar   apach   tin   get   logs   als   roo   

需要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

 

dbcp连接MySQL数据库

标签:except   too   jar   apach   tin   get   logs   als   roo   

原文地址:http://www.cnblogs.com/sun-hong/p/7573554.html

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