package net.fqq.student.dbutil;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
//数据路驱动程序
private static final String DRIVER="com.mysql.jdbc.Driver";
//数据统一资源标识符
private static final String URL="jdbc:mysql://localhost:3306/student?useSSL=false";
//数据库用户
private static final String USER="root";
//数据库密码
private static final String PASSWORD="123456";
private ConnectionManager() {}
public static Connection getConnection() {
//定义数据连接
Connection conn=null;
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL, USER, PASSWORD);
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn) {
if(conn!=null) {
try {
if(!conn.isClosed()) {
conn.close();
}
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String []args) {
Connection conn=getConnection();
if(conn!=null) {
JOptionPane.showMessageDialog(null,"恭喜,数据库链接成功!");
}
else {
JOptionPane.showMessageDialog(null, "遗憾,数据库链接失败!");
}
closeConnection(conn);
}
}
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at net.fqq.student.dbutil.ConnectionManager.getConnection(ConnectionManager.java:20)
at net.fqq.student.dbutil.ConnectionManager.main(ConnectionManager.java:44)
我粘贴你的代码连接成功了,你是不是数据库驱动什么的没下载好