日期:2014-05-16  浏览次数:20513 次

通过properties配置文件连接数据库
**
* @作者:Jcuckoo
* @日期:2008-11-8
* @版本:V 1.0
*/

db.properties


DBDriver=sun.jdbc.odbc.JdbcOdbcDriver
Connection=jdbc:odbc:login
User=Jcuckoo
Password=
dbPool.java

import java.io.*;
import java.util.*;
import java.sql.*;

public class dbPool{
    private static dbPool instance = null;

    //取得连接
    public static synchronized Connection getConnection() {
        if (instance == null){
            instance = new dbPool();
        }
        return instance._getConnection();
    }

    private dbPool(){
        super();
    }

    private  Connection _getConnection(){
        try{
            String sDBDriver  = null;
            String sConnection   = null;
            String sUser = null;
            String sPassword = null;

            Properties p = new Properties();
            InputStream is = getClass().getResourceAsStream("/db.properties");
            p.load(is);
            sDBDriver = p.getProperty("DBDriver",sDBDriver);
            sConnection = p.getProperty("Connection",sConnection);
            sUser = p.getProperty("User","");
            sPassword = p.getProperty("Password","");

            Properties pr = new Properties();
            pr.put("user",sUser);
            pr.put("password",sPassword);
            pr.put("characterEncoding", "GB2312");
            pr.put("useUnicode", "TRUE");

            Class.forName(sDBDriver).newInstance();
            return DriverManager.getConnection(sConnection,pr);
        }
        catch(Exception se){
            System.out.println(se);
            return null;
        }
    }

    //释放资源
    public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)
    throws SQLException
    {
          rs.close();
          ps.close();
          conn.close();

    &nb