执行后这是原因呢???
package JDBC;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class login {
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String username = scanner.next();
System.out.println("请输入密码");
String password = scanner.next();
Class.forName("com.mysql.jdbc");
Connection connection = DriverManager.getConnection("jdbc:mysql://local:3306/companydb","root","1234");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from users where username='"+username+"' and password ='"+password+"'");
if (resultSet.next()){//查询到了数据
System.out.println("登录成功!");
}else {
System.out.println("登录失败!");
}
resultSet.close();
statement.close();
connection.close();
}
}
错误提示:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc
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 JDBC.login.main(login.java:16)
Process finished with exit code 1
请问各位大咖,这是什么原因呢??? Class.forName("com.mysql.jdbc")
应该改为Class.forName("com.mysql.jdbc.Driver") 在程序加个try /catch抛一下异常就行了
请确保已经导入MySQL JDBC驱动程序,使用Class.forName("com.mysql.jdbc.Driver");完成加载
页:
[1]