问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
package sql;
import java.sql.*;
public class Mysql {
Connection in;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
in=DriverManager.getConnection("jdbc:mysql:"+"//127.0.0.1:3306/test2","root","kscl");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return in;
}
public static void main(String[] args) {
Mysql a=new Mysql();
a.getConnection();
}
}
运行结果及报错内容
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:377)
at sql.Mysql.getConnection(Mysql.java:10)
at sql.Mysql.main(Mysql.java:25)
java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/test2
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at sql.Mysql.getConnection(Mysql.java:16)
at sql.Mysql.main(Mysql.java:25)
我的解答思路和尝试过的方法
尝试过导入mysql-connector-java-5.1.46-bin.jar也没用
我想要达到的结果
可以运行就行