如何使用java连接mysql数据库

如题所述

public static Connection getConnection() {
Connection conn = null;

try {
String driver = "com.mysql.jdbc.Driver"; // 数据库驱动
String url = "jdbc:MySQL://127.0.0.1:3306/school";// 数据库
String user = "root"; // 用户名
String password = "hadoop"; // 密码
Class.forName(driver); // 加载数据库驱动
if (null == conn) {
conn = DriverManager.getConnection(url, user, password);
}
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can't find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答