[英]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.如果问题仍然存在,请按照以下步骤创建新项目。
Ctrl + Shift + P to open the command palette, search for and select Java:Create Java Project... Ctrl + Shift + P打开命令面板,搜索 select Java:创建 Java 项目...
Then Select No Build tools --> select a folder --> type the project name ( Screenshot of steps )然后 Select没有构建工具--> select 一个文件夹 --> 输入项目名称(步骤截图)
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
Type the class name ( newapp2jdbc
) and hit Enter输入 class 名称 (
newapp2jdbc
) 并按Enter
Paste the code from your question in the generated newapp2jdbc.java file将问题中的代码粘贴到生成的newapp2jdbc.java文件中
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官网下载。
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.