public class DBUTtil {
public static Connection jdbaload() {
Properties prop=new Properties();
Connection conn=null;
try {
prop.load(DBUTtil.class.getClassLoader().getResourceAsStream("config.properties"));
} catch (IOException e1) {
System.out.println("文件为读取成功!");
}
String url=prop.getProperty("url");
String driver=prop.getProperty("driver");
String user=prop.getProperty("user");
String password=prop.getProperty("password");
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println("未加载mysql驱动!");
} catch (SQLException e) {
System.out.println("未连接mysql!");
}
if(conn!=null) {
return conn;
}
else {
throw new Error("数据库连接错误!");
}
}
}
config.properties文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/uesrbase
user=root
password=root