```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);
}
}
```