数据库连接池

package dbcpool;

import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
/**
 * 适用于多线程数据库操作
 * @author Fly
 *
 */
public class DBUtils {
    private static String driver ;
    private static String url;
    private static String user;
    private static String password;
    private static int initSize;
    private static int maxActive;
    private static BasicDataSource bs;//管理数据库连接的实现类
    //静态方法创建连接池
    static{


        bs = new BasicDataSource();
        Properties pro = new Properties();
        try {
            InputStream in = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
            pro.load(in);
            driver = pro.getProperty("jdbc.driver");
            url = pro.getProperty("jdbc.url");
            user = pro.getProperty("jdbc.user");
            password = pro.getProperty("jdbc.password");
            initSize = Integer.parseInt(pro.getProperty("initSize"));
            maxActive = Integer.parseInt(pro.getProperty("maxActive"));
            in.close();
            bs.setDriverClassName(driver);//设置管理类的相关属性
            bs.setUrl(url);
            bs.setUsername(user);
            bs.setPassword(password);
            bs.setInitialSize(initSize);
            bs.setMaxActive(maxActive);
        } catch (Exception e) {
        }
    }
    public static Connection getConnection() {
        try {
            //bs.getConnection()从连接池中获取连接
            Connection conn = bs.getConnection();
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    public static void close(Connection conn) {
        try {
            if(conn!=null){
                //归还连接池
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

package dbcpool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo1 {//多线程的模拟效果
    public static void main(String[] args) throws Exception {
    
    Thread thread1 = new Thread(new Runnable() {
        
        public void run() {
            try {
                Thread.sleep(2000);
                Connection conn = dbcpool.DBUtils.getConnection();
                System.out.println("开始调用第1个线程");
                Statement st = conn.createStatement();
                String sql = "select 'hello' as a from dual ";
                ResultSet rs = st.executeQuery(sql);
                while (rs.next()) {
                    String string = rs.getString("a");
                    System.out.println(string);
                }
                st.close();
                rs.close();
                conn.close();
                System.out.println("第1条线程归还");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    Thread thread2= new Thread(new Runnable() {
        
        public void run() {
            try {
                Thread.sleep(2000);
                Connection conn = dbcpool.DBUtils.getConnection();
                System.out.println("开始调用第2个线程");
                Statement st = conn.createStatement();
                String sql = "select 'hello' as a from dual ";
                ResultSet rs = st.executeQuery(sql);
                while (rs.next()) {
                    String string = rs.getString("a");
                    System.out.println(string);
                }
                st.close();
                rs.close();
                conn.close();
                System.out.println("第2条线程归还");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    Thread thread3 = new Thread(new Runnable() {
        
            public void run() {
                try {
                    Thread.sleep(2000);
                    Connection conn = dbcpool.DBUtils.getConnection();
                    System.out.println("开始调用第3个线程");
                    Statement st = conn.createStatement();
                    String sql = "select 'hello' as a from dual ";
                    ResultSet rs = st.executeQuery(sql);
                    while (rs.next()) {
                        String string = rs.getString("a");
                        System.out.println(string);
                    }
                    st.close();
                    rs.close();
                    conn.close();
                    System.out.println("第3条线程归还");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值