1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.SQLException;
4 public class 类名 {
5 Connection con;
6 public Connection getConnection() {
7 try {
8 Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("数据库驱动加载成功");
9 } catch(ClassNotFoundException e){
10 e.printStackTrace();
11 }
12 try {
13 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql?serverTimezone=UTC&characterEncoding=utf-8","用户名","密码");
14 System.out.println("数据库连接成功");
15 } catch (SQLException e) {
16 e.printStackTrace();
17 }
18 return con;
19 }
20 public static void main(String[] args) {
21 类名 c = new 类名();
22 c.getConnection();
23 }
24 }