Oracle--jdbc创建连接池,jdbc连接数据库

开发者博客www.developsearch.com

package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.LinkedList;

public class DBConnetion {

    public static DBConnetion dbconn = null;
    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://127.0.0.1:3306/project?useUnicode=true&characterEncoding=gbk";
    private String username = "username ";
    private String password = "password ";
    private LinkedList<Connection> pool = new LinkedList<Connection>();
    private int initCount = 10;
    private int maxCount = 20;
    int currentCount = 0;

    public static void main(String[] args) {
        try {
            long time1 = new Date().getTime();
            dbconn = new DBConnetion();
            for(int i = 1;i<= 20;i++){
                DBThreadTest dt = new DBThreadTest();
                dt.start();
            }
            long time3 = new Date().getTime();
            System.out.println(time3 - time1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    public DBConnetion() {
        try {
            Class.forName(driver); // 加载驱动
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < initCount; i++) {
            this.pool.addLast(createConnection());
            this.currentCount++;
        }
    }
   
    public static boolean executeBySql(String sql){
        try {
            Connection conn = dbconn.getConnection();
            Statement stat=conn.createStatement();
            int num = stat.executeUpdate(sql);
            stat.close();
            return num>0?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public Connection getConnection() {
        synchronized (pool) {
            try {
                if (this.pool.size() > 0) {
                    Connection conn = pool.removeFirst();
                    removeConn(conn);
                    return conn;
                }
                if (this.currentCount < maxCount) {
                    return createConnection();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return getConnection();
            }
            return getConnection();
        }
    }

    public void removeConn(Connection conn) {
        pool.addLast(conn);
    }

    public Connection createConnection() {
        Connection conns = null;
        try {
            conns = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.out.println("创建链接失败");
        }
        return conns;
    }

    public LinkedList<Connection> getPool() {
        return pool;
    }

    public void setPool(LinkedList<Connection> pool) {
        this.pool = pool;
    }

}

猜你喜欢

转载自keepwork.iteye.com/blog/1944767