import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
private static final String username = "root";//用户名
private static final String pwd = "root";//密码
private static final String driverClass = "com.mysql.jdbc.Driver";//驱动类名
static String url = "jdbc:mysql://localhost:3306/game_king?characterEncoding=utf-8";//连接地址
public static Connection getConn() throws ClassNotFoundException, SQLException {
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(url, username, pwd);
if (conn != null) {
System.out.println("建立连接成功!");
return conn;
} else {
System.out.println("建立连接失败!");
return null;
}
}
}
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver {
public static void main(String[] args) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/game_king", "root", "root");
System.out.println("建立连接成功");
} catch (Exception ex) {
System.out.println("建立连接失败");
} finally {
if (con != null) {
con.close();
}
}
}
}
jdbc连接吗?
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gameking","root","root");
```java
再导入mysql的jar包,外面套上异常就行了
```
这是一道作业,
就算其他人给了你代码,
你怕理解也为难。
常言道,
读万卷书,不如行万里路;
行万里路,不如高人指路。
有问题,欢迎来交流。