JDBC+MySQL连接池

news/2024/5/18 23:11:27 标签: jdbc, mysql, 数据库, 设计模式, url, path

 

1.创建一个java project项目pooling
2.为项目添加MySQL连接驱动
3.为项目添加一个配置文件dbpool.proprerties


driverClassName=com.mysql.jdbc.Driver
username=
root
password=

url
=jdbc:mysql://localhost:3306/work
poolSize=10


4.分别创建一个连接类ConnectionPool.java和一个测试类ConnectionPoolTest.java(代码)

 


package webbook.util;

import
 java.io.FileInputStream;
import
 java.sql.Connection;
import
 java.sql.SQLException;
import
 java.util.Properties;
import
 java.util.Vector;

public class
 ConnectionPool {

    private Vector<Connection>
 pool;

    private
 String url;

    private
 String username;

    private
 String password;

    private
 String driverClassName;

    /**

     * 连接池的大小,也就是连接池中有多少个数据库连接。
     
*/
    
private int poolSize = 1;

    private static ConnectionPool instance = null
;

    /**

     * 私有的构造方法,禁止外部创建本类的对象,要想获得本类的对象,通过<code>getIstance</code>方法。
     * 使用了设计模式中的单子模式。
     
*/
    
private ConnectionPool() {
        init();
    }

    /**

     * 连接池初始化方法,读取属性文件的内容 建立连接池中的初始连接
     
*/
    
private void init() {
        pool = new Vector<Connection>
(poolSize);
        readConfig();
        addConnection();
    }

    /**

     * 返回连接到连接池中
     
*/
    
public synchronized void release(Connection conn) {
        pool.add(conn);

    }

    /**

     * 关闭连接池中的所有数据库连接
     
*/
    
public synchronized void closePool() {
        for (int i = 0; i < pool.size(); i++
) {
            try
 {
                ((Connection) pool.get(i)).close();
            } catch
 (SQLException e) {
                e.printStackTrace();
            }
            pool.remove(i);
        }
    }

    /**

     * 返回当前连接池的一个对象
     
*/
    
public static ConnectionPool getInstance() {
        if (instance == null
) {
            instance = new
 ConnectionPool();
        }
        return
 instance;
    }

    /**

     * 返回连接池中的一个数据库连接
     
*/
    
public synchronized Connection getConnection() { 
        if (pool.size() > 0
) {
            Connection conn = pool.get(0
);
            pool.remove(conn);
            return
 conn;
        } else
 {
            return null
;
        }
    }

    /**

     * 在连接池中创建初始设置的的数据库连接
     
*/
    
private void addConnection() {
        Connection conn = null
;
        for (int i = 0; i < poolSize; i++
) {

            try
 {
                Class.forName(driverClassName);
                conn =
 java.sql.DriverManager.getConnection(url, username, password);
                pool.add(conn);

            } catch
 (ClassNotFoundException e) {
                e.printStackTrace();
            } catch
 (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    /**

     * 读取设置连接池的属性文件
     
*/
    
private void readConfig() {
        try
 {
            String path = System.getProperty("user.dir") + "//dbpool.properties"
;
            FileInputStream is = new
 FileInputStream(path);
            Properties props = new
 Properties();
            props.load(is);
            this.driverClassName = props.getProperty("driverClassName"
);
            this.username = props.getProperty("username"
);
            this.password = props.getProperty("password"
);
            this.url = props.getProperty("url"
);
            this.poolSize = Integer.parseInt(props.getProperty("poolSize"
));
        } catch
 (Exception e) {
            e.printStackTrace();
            System.err.println("读取属性文件出错. "
);        
        }
    }
}


 


package webbook.util;

import
 java.io.FileInputStream;
import
 java.sql.Connection;
import
 java.sql.SQLException;
import
 java.util.Properties;
import
 java.util.Vector;

public class
 ConnectionPool {

    private Vector<Connection>
 pool;

    private
 String url;

    private
 String username;

    private
 String password;

    private
 String driverClassName;

    /**

     * 连接池的大小,也就是连接池中有多少个数据库连接。
     
*/
    
private int poolSize = 1;

    private static ConnectionPool instance = null
;

    /**

     * 私有的构造方法,禁止外部创建本类的对象,要想获得本类的对象,通过<code>getIstance</code>方法。
     * 使用了设计模式中的单子模式。
     
*/
    
private ConnectionPool() {
        init();
    }

    /**

     * 连接池初始化方法,读取属性文件的内容 建立连接池中的初始连接
     
*/
    
private void init() {
        pool = new Vector<Connection>
(poolSize);
        readConfig();
        addConnection();
    }

    /**

     * 返回连接到连接池中
     
*/
    
public synchronized void release(Connection conn) {
        pool.add(conn);

    }

    /**

     * 关闭连接池中的所有数据库连接
     
*/
    
public synchronized void closePool() {
        for (int i = 0; i < pool.size(); i++
) {
            try
 {
                ((Connection) pool.get(i)).close();
            } catch
 (SQLException e) {
                e.printStackTrace();
            }
            pool.remove(i);
        }
    }

    /**

     * 返回当前连接池的一个对象
     
*/
    
public static ConnectionPool getInstance() {
        if (instance == null
) {
            instance = new
 ConnectionPool();
        }
        return
 instance;
    }

    /**

     * 返回连接池中的一个数据库连接
     
*/
    
public synchronized Connection getConnection() { 
        if (pool.size() > 0
) {
            Connection conn = pool.get(0
);
            pool.remove(conn);
            return
 conn;
        } else
 {
            return null
;
        }
    }

    /**

     * 在连接池中创建初始设置的的数据库连接
     
*/
    
private void addConnection() {
        Connection conn = null
;
        for (int i = 0; i < poolSize; i++
) {

            try
 {
                Class.forName(driverClassName);
                conn =
 java.sql.DriverManager.getConnection(url, username, password);
                pool.add(conn);

            } catch
 (ClassNotFoundException e) {
                e.printStackTrace();
            } catch
 (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    /**

     * 读取设置连接池的属性文件
     
*/
    
private void readConfig() {
        try
 {
            String path = System.getProperty("user.dir") + "//dbpool.properties"
;
            FileInputStream is = new
 FileInputStream(path);
            Properties props = new
 Properties();
            props.load(is);
            this.driverClassName = props.getProperty("driverClassName"
);
            this.username = props.getProperty("username"
);
            this.password = props.getProperty("password"
);
            this.url = props.getProperty("url"
);
            this.poolSize = Integer.parseInt(props.getProperty("poolSize"
));
        } catch
 (Exception e) {
            e.printStackTrace();
            System.err.println("读取属性文件出错. "
);        
        }
    }
}

5.进行测试。

引用:

http://www.cnblogs.com/java-zhu/articles/1264173.html


http://www.niftyadmin.cn/n/1051357.html

相关文章

char易错点

sizeof("Hello")是6而不是5 strlen("Hello")是5。 所以char str[sizeof("Hello")]; strcpy(str,"Hello");是正确的。

zzulioj 1734 堆

比赛的时候不会写&#xff0c;想不到DFS&#xff0c;一直以为需要二叉树或者建堆什么的&#xff0c;也没学&#xff0c;后来才明白这个题 代码&#xff1a; #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include…

Leetcode 21 Merge Two Sorted Lists

Leetcode 21 Merge Two Sorted Lists 题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2…

c++ 指针大小

学过数据结构的你应该知道指针是一个很重要的概念&#xff0c;它记录了另一个对象的地址。既然是来存放地址的&#xff0c;那么它当然等于计算机内部地址总线的宽度。所以在32位计算机中&#xff0c;一个指针变量的返回值必定是4&#xff08;注意结果是以字节为单位&#xff09…

Eclipse中“The selection is not within a valid module”异常处理

在工程目录下的.settings文件夹里&#xff0c;有个名为org.eclipse.wst.common.component 的文件。这个文件里的deploy-name&#xff0c;一定要跟工程名一样&#xff0c;否则就会报错 还有Eclipse窗体切换在Java ptesp。。下再启动&#xff2f;&#xff2b;

Leetcode 23 Merge k Sorted Lists

Leetcode 23 Merge k Sorted Lists 题目描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [1->4->5,1->3->4,2->6 ] Output: 1->1->2->3->4->4->5->6来源&…

洛谷 P1348 Couple number

题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗&#xff1f;如果能&#xff0c;那么这个数N就叫做Couple number。你的工作就是判断一个数N是不是Couple number。 输入输出格式 输入格式&#xff1a; 仅一行&#xff0c;两个长整型范围内的整数$n_1$和$n_2$&…

JMETER 命令行 执行(Non-GUI Mode)

non-gui&#xff1a;即表示没有图形化界面运行 不以图形化界面运行的&#xff0c; 1、没有图形化界面运行 2、先把jmeter的bin目录加入到环境变量里面------如果不添加环境变量&#xff0c;则D:\Jmeter\jmeter-3.1\bin\jmeter.bat 然后执行这个命令 jmeter -n -t e:\path\HTT…