Java 后台连接数据库配置文件

时间:2021-08-24 12:00:07

1.设置配置文件jdbc.properties(连接SQL server )

driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://192.168.0.
100:1433;databaseName=SPTERPData
username=sa
password=123456

2.将配置文件引入baseDao中

public BaseDao() {
props=new Properties();
try {
// 加载dbinfo.properties配置文件  
InputStream in=BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");
props.load(in);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
url=props.getProperty("url");
user=props.getProperty("username");
password=props.getProperty("password");
try {
Class.forName(props.getProperty("driver"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
}

public Connection getConnection(){
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
Log log=new Log();
log.getLogger();
}
return con;
}