-
- <?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);
- }
- }