java实现链接mysql数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.sql.*;

public class ConnectionMysql {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://192.168.101.1:13306/mysql?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "1qaz2wsx#EDC";

public Connection getConnection() {
Connection connection = null;
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("数据库连接成功");
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}

public static void closeConnection(Connection connection) {
try {
if (connection != null && !connection.isClosed()) connection.close();
System.out.println("数据库链接关闭");
} catch (SQLException e) {
e.printStackTrace();
}
}


}//class