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

JDBC第一天连接池案例

时间:2015-12-24 16:13:57      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

JDBC,JDBC的工具类JDBC

连接从连接池中拿:

创建连接池的语句:

技术分享
package day01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp2.BasicDataSource;

public class DBUtils {
    private static String driver = null;
    private static String url = null;
    private static String user = null;
    private static String password = null;
    
    private static BasicDataSource ds = null;
    //静态块
    static{
        Properties props = new Properties();
        try {
            //路径使用包路径
            String path = "day01/db.properties";
            props.load(
                       DBUtils.class.getClassLoader()
                       .getResourceAsStream(path));
            
            driver  = props.getProperty("driver");
            url  = props.getProperty("url");
            user  = props.getProperty("user");
            password  = props.getProperty("password");
            //ds中已经有了几个创建好的连接
            ds = new BasicDataSource();//创建连接池
            ds.setDriverClassName(driver);
            ds.setUrl(url);
            ds.setUsername(user);
            ds.setPassword(password);
            ds.setInitialSize(
                    Integer.parseInt(props.getProperty("intialSize")));
            
        
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /*创建连接*/
    public static Connection getConnection()
       throws ClassNotFoundException, SQLException{
        Connection conn = null;
        if(ds!=null){
             conn = ds.getConnection();
        }
        return conn;
    }
    
    
}
View Code

配置文件:

技术分享
#key = value
driver =   oracle.jdbc.OracleDriver
url =   jdbc:oracle:thin:@localhost:1521:xe
user =   fengweijie
password=   1070937053

#driver = com.mysql.jdbc.Driver
#url = jdbc:mysql:localhost:3306/test?useUni

intialSize = 10
View Code

 

JDBC第一天连接池案例

标签:

原文地址:http://www.cnblogs.com/fengdashen/p/5073284.html

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