论坛首页 入门技术论坛

JDBC工具类

浏览 3501 次
锁定老帖子 主题:JDBC工具类
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-01-09   最后修改:2009-01-09
import java.util.*;
import java.io.*; 
public class PropertyManager {
    private Properties properties = new Properties();
    File file;
    /** Creates a new instance of PropertyManager */
    public PropertyManager(String fileName) {
        String path = getClass().getResource("/").getPath();
        
        try{
            try{
                file = new File(path,fileName);
            }catch(Exception e){
                file = new File(path,"/"+fileName);
            }
            FileInputStream fis = new FileInputStream(file);
            properties.load(fis);
            fis.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public String get(String key){
        return properties.getProperty(key.trim());
    }
    
    public String get(String key,String defaultValue){
        return properties.getProperty(key.trim(), defaultValue);
    }
    
    public void set(String key,String value){
        try{
            properties.setProperty(key,value);//new String(value.getBytes("UTF-8"), "UTF-8")
        }catch(Exception e){
            properties.setProperty(key, value);
            e.printStackTrace();
        }
    }
    
    synchronized public boolean store(){
        String path = getClass().getResource("/").getPath();
        boolean b = false;
        try{
            FileOutputStream fos = new FileOutputStream(file);
            properties.store(fos,"");
            fos.close();
            b = true;
        }catch(Exception e){
            b = false;
            e.printStackTrace();
        }
        file = null;
        return b;
    }
    
    public static void main(String arg[]){
        String path = new Object().getClass().getResource("/").getPath();
        System.out.println(path);
    }
    
}

 

此类为读取*.properties的工具类

 

public class DataBassInfo {
    
    private String driver;
    private String url;
    private String userName; 
    private String password;
    
    /** Creates a new instance of DataBassInfo */
    public DataBassInfo() {
        
        PropertyManager pm = new PropertyManager("system.properties");
        driver = pm.get("DB_DRIVER");
        url = pm.get("DB_URL");
        userName = pm.get("DB_USERNAME");
        password = pm.get("DB_PASSWORD");
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
}

 

此类为读取*.properties内容的bean类

 

import java.sql.*;
import java.util.ArrayList;

public class ConnectionPool {
    protected static ConnectionPool connectionPool;
    protected String DBdriver;
    protected String DBurl;
    protected String DBuserName;
    protected String DBpassword;
    protected int maxConnection;
    protected int conNumber;
    protected ArrayList freeCon;
    
    /** Creates a new instance of ConnectionPool */
    protected ConnectionPool(DataBassInfo dbi){
        conNumber = 0;
        DBdriver = dbi.getDriver(); //"com.mysql.jdbc.Driver";
        DBurl = dbi.getUrl(); //"jdbc:mysql://localhost:3306/luru?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8";
        DBuserName = dbi.getUserName(); //"";
        DBpassword = dbi.getPassword(); //"";
        freeCon = new ArrayList();
        try {
            Class.forName(DBdriver);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        System.out.println(DBurl);
    }
    
    public synchronized Connection getConnection(){
        
        Connection con = null;
        try {
        	
        	
            if(!freeCon.isEmpty()){
                con = (Connection)freeCon.get(0);
                freeCon.remove(0);
            }else{
                if(maxConnection == 0 || conNumber<maxConnection){
                    con = DriverManager.getConnection(DBurl,DBuserName,DBpassword);
                    conNumber++;
                }else{
                    wait(2000);
                    return getConnection();
                }
            }
            
            if(con!=null&&con.isClosed()){
                conNumber--;
                return getConnection();
            }
            
        } catch (Exception ex) {
            System.out.println("CONNECT IS NULL");
            con = null;
            ex.printStackTrace();
        }
        return con;
    }
    
    public synchronized void restore(Connection con){
        if(con!=null){
        	try{
        		freeCon.add(con);
                notifyAll();
        	}catch (Exception ex) {
                con = null;
                ex.printStackTrace();
            }
        	
        }
    }
    
    public synchronized void closeAll(){
        while(!freeCon.isEmpty()){
            Connection con = (Connection)freeCon.get(0);
            freeCon.remove(0);
            try {
                con.close();
                conNumber--;
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public void setMaxConnection(int maxConnection){
        this.maxConnection = maxConnection;
    }
    
    public static ConnectionPool getInstance(){
        return getInstance(20);
    }
    
    public static synchronized ConnectionPool getInstance(int maxConnection){
        if(connectionPool == null){
            DataBassInfo dbi = new DataBassInfo();
            connectionPool = new ConnectionPool(dbi);
            connectionPool.setMaxConnection(maxConnection);
        }
        return connectionPool;
    }
    
    protected void finalize() throws Throwable{
        closeAll();
    }
    
}

 此类位处理连接的工具类

system.properties中的配置

DB_USERNAME=
DB_DRIVER=
DB_PASSWORD=
DB_URL=

四个属性对应DataBassInfo中的

 

使用即为调用ConnectionPool .getInstance()方法得到连接,然后就可以操作了。有点麻烦

   发表时间:2009-01-09  
不知道博主Property与JDBC有什么关系
0 请登录后投票
   发表时间:2009-01-09  
zzx0421 写道

不知道博主Property与JDBC有什么关系

不好意思没有写完Property读取jdbc的配置
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics