jdbc线程池

连接oracle数据库的jdbc线程池

首先建立一个properties类型的文件存放一些信息:jdbc.properties

1 driverClassName=oracle.jdbc.driver.OracleDriver
2 url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
3 username=teacher
4 password=ok
5 initialSize=1
6 maxActive=50
7 #maxIdle=8
8 #minIdle=1
9 maxWait=6000

此处建立一个工具类,专门为了建立连接,关闭连接,代码如下:

 1 package com.dao;
 2 
 3 import java.io.FileInputStream;
 4 import java.sql.Connection;
 5 import java.sql.SQLException;
 6 import java.util.Properties;
 7 
 8 import javax.sql.DataSource;
 9 
10 import org.apache.commons.dbcp.BasicDataSourceFactory;
11 
12 public class JdbcUtil {
13     private static DataSource ds;
14     static{
15         Properties p = new Properties();
16         try {
17                         //此处jdbc.properties文件使用的是绝对路径
18             p.load(new FileInputStream("E:\\甲骨文软件\\eclipse-jee-kepler-SR2-win32-x86_64\\WebWorkspace\\ServletTest\\src\\jdbc.properties"));
19             ds=BasicDataSourceFactory.createDataSource(p);
20         } catch (Exception e) {
21             // TODO Auto-generated catch block
22             e.printStackTrace();
23         }
24         
25     }
26     public static Connection getConnection(){
27         Connection conn=null;
28         try {
29             conn=ds.getConnection();
30         } catch (SQLException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34         return conn;
35     }
36     public static void CloseConnection (Connection conn){
37         if(conn!=null){
38             try {
39                 conn.close();
40             } catch (SQLException e) {
41                 // TODO Auto-generated catch block
42                 e.printStackTrace();
43             }
44         }
45     }
46 }
47                 

 

posted @ 2016-04-11 16:23  程序员的苦逼生活  阅读(1654)  评论(0编辑  收藏  举报