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
本文介绍了一个使用Java进行数据库连接的方法,通过加载配置文件中的数据库URL、驱动、用户名和密码等信息,实现数据库连接。
1137

被折叠的 条评论
为什么被折叠?



