未找到带有 VSCode 类的 JDBC

问题描述 投票:0回答:5

我在使用 VS Code 创建 Java 项目并添加“mysql-connector-java-8.0.21.jar”时遇到严重问题。 我已完成以下步骤:

  • 从命令行使用 VS Code 创建新的 Java 项目
  • 通过执行“添加引用的库”来添加 mysql 连接器。

我尝试使用 jdk 11 和 15(不是 8,因为 VS Code 不再支持它)

启动我的代码会导致错误:

java.lang.ClassNotFoundException: com.mysql.cj.LocalizedErrorMessages 

这是我的代码的摘录:

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SimpleJDBCApplication {

   static final String DB_URL = "jdbc:mysql://localhost:3306/company";
   static final String DB_DRV = "com.mysql.jdbc.Driver";
   static final String DB_USER = "root";
   static final String DB_PASSWD = "";

   public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

      Connection connection = null;
      Statement statement = null;
      ResultSet resultSet = null;

      try{
          /*To connect with a database using JDBC you need to select get the
                 driver for the respective database and register the driver.
                 The forName() method of the class named Class accepts a class name
                as a String parameter and loads it into the memory, Soon the is
                loaded into the memory it gets registered automatically  */  
                //Take new instance
         System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
         Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructor().newInstance();
         connection=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWD);
         statement=connection.createStatement();
         resultSet=statement.executeQuery ("SELECT * FROM dept");
         while(resultSet.next()){
            System.out.printf("%d\t%s\t%s\n",
            resultSet.getInt(1),
            resultSet.getString(2),
            resultSet.getString(3));

错误发生在

connection=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWD);

感谢您的帮助

已添加库 错误

java mysql jdbc visual-studio-code connection
5个回答
1
投票

1.适用于MySQL 8.0以下版本:

静态最终字符串 DB_URL = "jdbc:mysql://localhost:3306/company";

更改为

static final String DB_URL ="jdbc:mysql://localhost:3306/company?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"

2.将

com.mysql.jdbc.Driver
转移到
com.mysql.cj.jdbc.Driver

3.

Class.forName("com.mysql.cj.jdbc.Driver")
就够了,也有这段代码,
System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver")
不需要,可以评论或删除;

这对我有用,你可以尝试一下。


0
投票

根据文档,类名应该是

com.mysql.cj.jdbc.Driver
而不是
com.mysql.jdbc.Driver
。另外,对
getDeclaredConstructor()
的调用似乎是不必要的。也许这些就是你问题的根源。


0
投票

enter image description here单击 VSCode 左侧资源管理器选项卡中的“Java 项目”。然后右键单击您的项目名称,然后单击“配置类路径”。这将在新选项卡中打开类路径配置。滚动到底部,然后单击引用的库上的“添加”。这将打开一个资源管理器弹出窗口。选择 java-mysql 连接器 jar 文件,然后它应该可以工作。


0
投票

步骤1)从VS Code左下角打开java项目

第 2 步)单击引用库上的 + 按钮

步骤 3) 浏览驱动程序,即本例中的连接器文件。

第 4 步)问题解决并创建连接


0
投票
  1. connector jar file
    添加到
    Referenced Libraries
  2. src
    内创建嵌套文件夹,例如
    com/myj
    。因此,创建一个包。
  3. 将 App.java 文件移至
    myj
    文件夹中。
  4. src
    文件夹下创建module-info.java。 Reference Image of module-info.java
  5. 使用
    Run Java
    选项运行代码。 Reference Image of Run Java
  • 注意:请勿使用
    Run Code
    选项,因为它可能无法正确处理模块依赖项。
© www.soinside.com 2019 - 2024. All rights reserved.