java - 在java中获取java.lang.ClassNotFoundException : com. mysql.jdbc.Driver

标签 java mysql jdbc

我正在尝试连接到数据库。之前它工作正常。但现在有些 jar 被删除了或者我不知道的东西(红十字会出现在它们身上)。但我再次下载了所有 jars (mysql-connector,commons-io-2.4),但我仍然在 Class.forName("com.mysql.jdbc.Driver"); 处收到 classnotfound 异常; 这是我的代码

    package abc;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

     public class JDBC_oracle {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //step1 load the driver class  
    Class.forName("com.mysql.jdbc.Driver");
    //step2 create  the connection object  
    Connection  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd");
    //step3 create the statement object  
    Statement statement = connection.createStatement();
    statement.executeUpdate("sql_query"); 
    ResultSet rs=statement.executeQuery("sql_query");       
    while(rs.next())  
        System.out.println("I am triyng to print data");
//step5 close the connection object 
    connection.close();  
}
  }

这是错误

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
   at java.net.URLClassLoader$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
 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 deiniteguide.JDBC_example.main(JDBC_example.java:24)

我尝试过使用 try catch 但它对我不起作用。

最佳答案

如果您使用的是 netbeans,您可以转到项目中的库文件夹。右键单击并添加库并选择 mysql 驱动程序。抱歉,由于声誉较低,无法发表评论,如果您可以发布您收到的错误消息,会更容易。另外在程序中使用try catch,这样会更容易定位错误。您的代码可以是这样的:

  public class JDBC_oracle {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //step1 load the driver class  
     try{
    Class.forName("com.mysql.jdbc.Driver");
       }catch(Exception e){
         System.out.println("Class not found" +e);
    //step2 create  the connection object  
     try{
    Connection  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd");
     }catch(Exception es){
     System.out.println("connection couldn't be made "+es);
      }
    //step3 create the statement object  
     try{
    Statement statement = connection.createStatement();
    statement.executeUpdate("sql_query"); 
    ResultSet rs=statement.executeQuery("sql_query");   }
     catch(Exception er){
      System.out.println("Query wasnot executed "+er);    
     }
    while(rs.next())  
        System.out.println("I am triyng to print data");
//step5 close the connection object 
    connection.close();  
}
  }

关于java - 在java中获取java.lang.ClassNotFoundException : com. mysql.jdbc.Driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359853/

相关文章:

php - 根据同一个表的查询更新表

sql-server - JDBC/IntelliJ 加载 sqljdbc_auth.dll 失败

java - 代码编译正常,但 JVM 抛出 NullPointerException

java - 矩阵表达式解析器/引擎

c# - Mysql同时读取和更改变量

php - 页面被重定向到错误的页面

postgresql - 在docker环境下为tcp md5配置Postgres

java - 从多个类访问用户数据库

java - Hibernate Search 空间构面查询首先运行 5 次,然后仅返回第 5 个结果

java - 为什么在更改 Android 中的区域时 DateTimeZone.getDefault() 不会更新