51,410
社区成员
发帖
与我相关
我的任务
分享//方便大家阅读
public class JDBCUtilsConfig {
private static Connection con ;
private static String driverClass;
private static String url;
private static String username;
private static String password;
/*public static void main(String[] args) throws Exception{
readConfig();
System.out.println(driverClass);
System.out.println(url);
System.out.println(username);
System.out.println(password);
//这里打印的几项确定没有问题
}*/
static {
try {
readConfig();
Class.forName(driverClass);
con = DriverManager.getConnection(url, username, password);
}catch(Exception ex) {
throw new RuntimeException(ex+"连接数据库失败");
}
}
private static void readConfig() throws Exception{
InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
Properties pro = new Properties();
pro.load(in);
driverClass=pro.getProperty("driverClass");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
}
public static Connection getConnection() {
return con;
}