📄 dbconn.java
字号:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.io.InputStream;
import java.util.Properties;
public class dbconn {
String driverName=null;//数据库驱动名
String connString=null;//连接字符串
String userName=null;//用户名
String password=null;//密码
String propertyFileName=null;//文件名
public dbconn(){
}
public Connection getDBConn(){//得到连接数据库对象
this.setPropertyFileName("/dbconn.properties");
driverName=this.getPropertyFromFile("driverName");
connString=this.getPropertyFromFile("connString");
userName=this.getPropertyFromFile("userName");
password=this.getPropertyFromFile("password");
if(driverName==null||connString==null||userName==null) return null;
try{
//Connection connDBObject=null;
Class.forName(driverName);
return DriverManager.getConnection(connString,userName,password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public String getConnString(){
return connString;
}
public String getDriverName(){
return driverName;
}
public String getUserName(){
return userName;
}
public String getPassword(){
return password;
}
public void setPropertyFileName(String propertyFileName){
this.propertyFileName = propertyFileName;
}
public String getPropertyFileName(){
return propertyFileName;
}
public String getPropertyFromFile(String refName){
if(this.getPropertyFileName()==null) return new String("");
try{//从.properties文件中得到refName属性的值
InputStream fin = getClass().getResourceAsStream(this.getPropertyFileName());
Properties props = new Properties();
props.load(fin);
return (String)props.getProperty(refName);
}catch(Exception e){
e.printStackTrace();
return new String("");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -