ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```java package com.gosuncn; import java.sql.Connection; import java.sql.DriverManager; public class ConnectionMain { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); System.out.println(connect); } } ``` com.mysql.jdbc.Driver在加载时已经在DriverManager中注册过了。 ```java static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException e) { throw new RuntimeException("Can't register driver!"); } } ``` # 更加牛逼 ```java Class.forName("com.mysql.jdbc.Driver"); ``` 也可以省略。 ```java package com.gosuncn; import java.sql.Connection; import java.sql.DriverManager; public class ConnectionMain { public static void main(String[] args) throws Exception { // Class.forName("com.mysql.jdbc.Driver"); Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); System.out.println(connect); } } ```