-
- <?xml version="1.0" encoding="gb2312"?>
- <driver xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <dbmsdriver name="华工达梦数据库系统">
- <driverclass>dm.jdbc.driver.DmDriver</driverclass>
- <url>jdbc:dm://localhost:12345/guest</url>
- <username>SYSDBA</username>
- <password>123456</password>
- <maxconnection>9</maxconnection>
- <minconnection>5</minconnection>
- <logpath>..//poolserver.log</logpath>
- </dbmsdriver>
- <!-- this program copyright by mengdejun -->
- </driver>
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Date;
- import java.util.Vector;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- /**
- * @see Connectionpool java连接池.
- * @author 武汉软件工程职业学院<br>
- * 孟德军<br>
- * 2009-01-01
- * @version 1.0
- */
- public class Connectionpool {
- /**
- * @see #logpath 日志文件存放的路径.
- */
- private String logpath = "//server.log";
- /**
- * @see #log 记录连接池异常信息.
- */
- private static PrintWriter log;
- /**
- * @see #file 日志文件
- */
- private File file;
- /**
- * @see #driverclass 数据库驱动类,默认mysql5.0
- */
- private String driverclass = "com.mysql.jdbc.Driver";
- /**
- * @see #url 数据库连接地址,默认:jdbc:mysql://localhost:3306/guest
- */
- private String url = "jdbc:mysql://localhost:3306/guest";
- /**
- * @see #username 数据库用户名,默认:root
- */
- private String username = "root";
- /**
- * @see #password 数据库密码,默认:123456
- */
- private String password = "123456";
- /**
- * @see #maxconnection 连接池的最大容量,默认:10
- */
- private int maxconnection = 10;
- /**
- * @see #minconnection 连接池的最大容量,默认:5
- */
- private int minconnection = 5;
- /**
- * @see #pool 连接池容器
- */
- private Vector pool = null;// 用于放连接池的容器
- private Connection con = null;//
- private static String path = "";
- /**
- * @see #instance connectionpool:唯一实例,可通过getinstance获得.
- */
- private static Connectionpool instance = null;
- /**
- * @see #Connectionpool() 私有构造方法.
- */
- private Connectionpool() {
- init();
- addConnection();
- }
- /**
- * @see #readconfig
- */
- private void init() {
- readconfig(path);
- pool = new Vector(maxconnection);
- try {
- file = new File(logpath);
- log = new PrintWriter(new FileWriter(file.getAbsolutePath(), true),
- true);
- serverlog(new Date() + ":/t server start /n");
- } catch (IOException e) {
- e.printStackTrace();
- serverlog(new Date() + ":/t" + e.getMessage() + "/n");
- }
- }
- /**
- *
- * @see #getinstance(String) 获取连接池的实例
- * @see #path 配置文件路径.
- * @return #Connectionpool 类的实例.
- */
- public synchronized static Connectionpool getinstance(String path) {
- Connectionpool.path = path;
- if (instance == null) {
- instance = new Connectionpool();
- }
- return instance;
- }
- /**
- *
- * @return 连接池中的连接.
- */
- public synchronized Connection getconnection() {
- if (pool.size() > 0) {
- con = (Connection) pool.get(0);
- pool.remove(0);
- checkpool(pool);
- return con;
- } else {
- return null;
- }
- }
- /**
- *
- * @param con 用户当前使用的连接
- */
- public synchronized void releaseconnection(Connection con) {
- pool.add(con);
- }
- /**
- * @see #closeconnectionpool(Connection) 关闭连接池.
- * @param con 用户当前使用的连接
- * @throws SQLException
- */
- public synchronized void closeconnectionpool() {
- for (int i = 0; i < pool.size(); i++) {
- try {
- ((Connection) pool.get(i)).close();
- pool.remove(i);
- serverlog(new Date() + ": /tserver close /n");
- } catch (SQLException e) {
- e.printStackTrace();
- serverlog(new Date() + ":/t" + e.getMessage() + "/n");
- }
- }
- }
- /**
- * @see #addConnection() 连接池初始化,为连接池创建连接.
- */
- private void addConnection() {
- for (int i = 0; i < minconnection; i++) {
- try {
- Class.forName(driverclass);
- con = DriverManager.getConnection(url, username, password);
- pool.add(con);
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- serverlog(new Date() + ":/t class not found" + e.getMessage()
- + "/n");
- } catch (SQLException e) {
- e.printStackTrace();
- serverlog(new Date() + ":/t" + e.getMessage() + "/n");
- }
- }
- }
- /**
- * @see #readconfig(String) 读取配置文件.<br>
- * 初始化数据库连接数据.
- * @since http://blog.csdn.net/mak0000
- * <a href="http://blog.csdn.net/mak0000">更多信息</a>
- * @throws path 为配置文件路径,文件路径错误会抛出FileNotFoundException异常
- */
- private void readconfig(String path) {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(Connectionpool.path);
- NodeList nodelist = document.getElementsByTagName("dbmsdriver");
- for (int i = 0; i < nodelist.getLength(); i++) {
- Element element = (Element) nodelist.item(i);
- driverclass = element.getElementsByTagName("driverclass").item(
- 0).getFirstChild().getNodeValue();
- url = element.getElementsByTagName("url").item(0)
- .getFirstChild().getNodeValue();
- username = element.getElementsByTagName("username").item(0)
- .getFirstChild().getNodeValue();
- password = element.getElementsByTagName("password").item(0)
- .getFirstChild().getNodeValue();
- maxconnection = Integer
- .parseInt(element.getElementsByTagName("maxconnection")
- .item(0).getFirstChild().getNodeValue());
- minconnection = Integer
- .parseInt(element.getElementsByTagName("minconnection")
- .item(0).getFirstChild().getNodeValue());
- logpath = element.getElementsByTagName("logpath").item(0)
- .getFirstChild().getNodeValue();
- }
- } catch (ParserConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- serverlog(new Date() + ":/t" + e.getMessage() + "/n");
- } catch (SAXException e) {
- e.printStackTrace();
- serverlog(new Date() + ":/t" + e.getMessage() + "/n");
- } catch (IOException e) {
- e.printStackTrace();
- serverlog(new Date() + ":/t" + e.getMessage() + "/n");
- }
- }
- /**
- * @see #checkpool(Vector) 检查连接池,并适当的为连接池添加一定数量的连接.回收无用的连接.
- * @param pool
- * 连接池.
- */
- private void checkpool(Vector pool) {
- Vector checkpool = null;
- checkpool = pool;
- if (checkpool.size() < minconnection) {
- while (pool.size() < maxconnection) {
- addConnection();
- }
- }
- }
- /**
- * @see #serverlog(String) 记录连接池运行信息.
- * @param msg
- * 异常信息.
- */
- public void serverlog(String msg) {
- log.println(msg);
- log.close();
- }
- }
- package com.mdj.test;
- import java.sql.Connection;
- import java.sql.SQLException;
- import com.mdj.connectionpool.Connectionpool;
- public class Test {
- public Test(String path) {
- Connectionpool pool = Connectionpool.getinstance(path);
- //请正确修改配置文件.
- Connection con = pool.getconnection();
- /**
- *你可以在这里书写的SQL语句.
- *
- */
- //释放连接.将连接返回给连接池。
- pool.releaseconnection(con);
- //关闭连接池.关闭服务器时使用.
- pool.closeconnectionpool(con);
- }
- public static void main(String[] args) {
- String path = System.getProperty("user.dir") + "//sysconfig.xml";
- new Test(path);
- }
- }