简体   繁体   English

线程“主”中的异常 java.lang.ClassNotFoundException: com.mysql.Z84BEFFD3A00D496736 中已添加引用库 jar 文件,错误仍然存在

[英]Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in vscode. have added referenced libraries jar file, error persists

Here is a little insight, I have reinstalled VS Code and its extensions, used this code without any extensions, and the error remains the same.这里有一点见解,我已经重新安装了 VS Code 及其扩展,在没有任何扩展的情况下使用了这个代码,错误仍然是一样的。

I also added the jar file in referenced libraries and refreshed it, but the code doesn't work.我还在引用的库中添加了 jar 文件并刷新了它,但代码不起作用。

I tried importing com.mysql.jdbc.Driver and com.mysql.cj.jdbc.Driver , tried changing com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver . I tried importing com.mysql.jdbc.Driver and com.mysql.cj.jdbc.Driver , tried changing com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver .

This code works in eclipse IDE, but I would rather not switch my entire operation to eclipse.此代码适用于 eclipse IDE,但我宁愿不将整个操作切换到 eclipse。

import java.sql.*;

public class newapp2jdbc {
    public static void main(String[] args) throws Exception {
        

    String url = "jdbc:mysql://localhost:3306/sql_workbench";
    String uname = "root";
    String pass = "0768";
    String query = "select name from student where id=3";

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url, uname, pass);
    Statement st = con.createStatement();

    ResultSet rs = st.executeQuery(query);

    rs.next();
       
    String name = rs.getString(1);
    System.out.println(name);
    st.close();
    con.close();
    }
}

First try to use the command Java:Clean Language Server Workspace in the command palette to see if the problem can be solved.首先尝试使用命令面板中的命令Java:Clean Language Server Workspace看看能否解决问题。

在此处输入图像描述

If the problem persists, follow the steps below to create a new project.如果问题仍然存在,请按照以下步骤创建新项目。

  1. Ctrl + Shift + P to open the command palette, search for and select Java:Create Java Project... Ctrl + Shift + P打开命令面板,搜索 select Java:创建 Java 项目...

    在此处输入图像描述

  2. Then Select No Build tools --> select a folder --> type the project name ( Screenshot of steps )然后 Select没有构建工具--> select 一个文件夹 --> 输入项目名称(步骤截图

  3. Now that you have a new Java project, in the JAVA PROJECTS panel click the plus sign after src to create a new class现在你有了一个新的 Java 项目,在JAVA PROJECTS面板点击src后面的加号创建一个新的 class

    在此处输入图像描述

  4. Type the class name ( newapp2jdbc ) and hit Enter输入 class 名称 ( newapp2jdbc ) 并按Enter

    在此处输入图像描述

  5. Paste the code from your question in the generated newapp2jdbc.java file将问题中的代码粘贴到生成的newapp2jdbc.java文件中

  6. Click the plus sign to the right of Referenced Libraries to add dependencies点击Referenced Libraries右边的加号添加依赖

    在此处输入图像描述

    Here is mysql-connector-java-8.0.30.jar for me, you can download it from the MySQL official website.我这里是mysql-connector-java-8.0.30.jar ,你可以从MySQL官网下载。

  7. The code can now be run.现在可以运行代码了。

    在此处输入图像描述

Note: The full Extension Pack for Java needs to be installed注意:需要安装Java 的完整扩展包

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Eclipse:我已经添加了JAR文件。 java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - Eclipse: I have added the JAR file. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.driver 错误,jar 文件已经导入 - java.lang.ClassNotFoundException: com.mysql.jdbc.driver error with jar file already imported 错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver? - error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver? 错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - Error : java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 通过命令行运行JAR时发生错误java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - Error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver while running JAR through Command line Runtime error while executing JAR file from CMD: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver - Runtime error while executing JAR file from CMD: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.ClassNotFoundException:Eclipse中的com.mysql.jdbc.Driver,即使已引用mysql-connector jar - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse even though the mysql-connector jar is referenced java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse Java.lang.ClassnotfoundException com.mysql.jdbc.Driver - Java.lang.ClassnotfoundException com.mysql.jdbc.Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM