一个Java连数据库的轮子

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

import com.toScience.Function;

public class DBConnection
{

private String FileName;            //配置文件名
private int DBType;                //数据库类型
private Connection conn;            //连接对象
private String MySqlDriver;            //MYSQL Server驱动程序
private String MySqlURL;             //MYSQL Server连接字符串
    
public DBConnection(){
    conn = null;
}

public  Connection getConn()
{
    DBType= new Function().StrToInt(getPara("DBType"));

    switch(DBType){
        case 1:return(getConnToMySql());
        default:return null;
    }    
}

public String getPara(String ParaName){
    FileName="../DBConfig.property";
    Properties prop= new Properties();
    try{
        InputStream is=getClass().getResourceAsStream(FileName);
        prop.load(is);
        if(is!=null) is.close();
    }
    catch(Exception e) {
        return "Error!";
    }
    return prop.getProperty(ParaName);
}

public Connection getConnToMySql(){
    try{
         MySqlDriver = getPara("MySQLDriver");    
        MySqlURL = getPara("MySQLURL");
        Class.forName(MySqlDriver).newInstance();

// Class.forName("com.mysql.jdbc.Driver");

        conn = DriverManager.getConnection(MySqlURL);
        
        }catch(Exception e){
            e.printStackTrace();
            //return "操作数据库出错,请仔细检查" ;
            System.err.println(e.getMessage());
        }
    return conn;
}       

}


扫描二维码,在手机上阅读!

发表评论

电子邮件地址不会被公开。 必填项已用*标注