import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
* 程序功能:取得数据库的连接
* @author Administrator
*
*/
public class ChinaLogin {
private static Logger log=Logger.getLogger(ChinaLogin.class);
static Properties prop=null;
static String driver;
static String url;
static String user;
static String pwd;
static{
InputStream ins=null;
prop=new Properties();
ins=ChinaLogin.class.getResourceAsStream("./db.properties");
if(null == ins){
log.debug("文件不存在");
}else{
try {
prop.load(ins);
driver=prop.getProperty("driver.class");
url=prop.getProperty("url");
user=prop.getProperty("user.name");
pwd=prop.getProperty("user.password");
} catch (IOException e) {
log.debug(e.getMessage());
}
}
}
// 取得数据库的连接
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
log.debug("注册失败"+e.getMessage());
} catch (SQLException e) {
log.debug("连接失败"+e.getMessage());
}
return conn;
}
// 测试
/*
public static void main(String[] args){
Connection conn=null;
conn=ChinaLogin.getConnection();
if(conn!=null){
log.debug("成功");
}
}
*/
}