JDBC在VSCode中找不到类。

huangapple go评论109阅读模式
英文:

JDBC with VSCode class not found

问题

我在使用VS Code创建Java项目并添加"mysql-connector-java-8.0.21.jar"方面遇到了严重的问题。

我已经完成了以下步骤:

  • 通过命令行在VS Code中创建了一个全新的Java项目。
  • 通过执行"Add referenced libraries"来添加了mysql连接器。

我尝试过使用JDK 11和15(因为VS Code不再支持JDK 8)。

运行我的代码会导致错误: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 {
          // 连接到数据库的JDBC代码
         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));
         }
      } catch (SQLException e) {
          // 处理异常
      } finally {
          // 关闭资源
      }
   }
}

错误出现在这一行:connection=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWD);

谢谢您的帮助。

已添加的库
错误截图

英文:

I am having serious trouble creating a Java Project with VS Code with adding the "mysql-connector-java-8.0.21.jar".
I have done the following steps:

  • Creating a fresh Java project with VS Code from the command line
  • Adding the mysql connector by doing "Add referenced libraries".

I tried using both jdk 11 and 15 (not 8 since VS Code doesn't support it anymore)

Launching my code result in the error: java.lang.ClassNotFoundException: com.mysql.cj.LocalizedErrorMessages

Here is an extract of my code:

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));

The error occures at the line connection=DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWD);

Thank you for any help

Library added
Error

答案1

得分: 1

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

    static final String 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"),你可以将其注释或删除;

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

英文:

1.This is applicable to MySQL version below 8.0:
> static final String DB_URL = "jdbc:mysql://localhost:3306/company";

Change it to

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

2.Transfer com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver;

3.Class.forName("com.mysql.cj.jdbc.Driver") is enough, also with this code, System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver") isn't needed, you can comment or delete it;

This works for me and you can have a try.

答案2

得分: 0

根据文档,类名应为com.mysql.cj.jdbc.Driver,而不是com.mysql.jdbc.Driver。此外,对getDeclaredConstructor()的调用似乎是不必要的。也许这些是问题的源头。

英文:

According to the documentation the class name should be com.mysql.cj.jdbc.Driver instead of com.mysql.jdbc.Driver. Also the call to getDeclaredConstructor() seems to be unnecessary. Maybe those are the source of your problems.

答案3

得分: 0

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

英文:

JDBC在VSCode中找不到类。Click on Java Projects in the explorer tab on the left-hand side in VSCode. Then right-click on your project name and click on Configure Classpath. this will open Classpath Configuration in a new tab. Scroll to the bottom and then click add on the referenced libraries. This will open an explorer pop-up window. Select the java-mysql connector jar file and then it should work.

答案4

得分: 0

Step 1) 打开 VS Code 左下角的 Java 项目。

Step 2) 点击引用库中的 + 按钮。

Step 3) 浏览并选择驱动程序,即此情况下的连接器文件。

Step 4) 问题解决,连接已创建。

英文:

Step 1) Open java projects from bottom left of VS Code

Step 2) Click on + button on refrenced libraries

Step 3) Browse for the driver i.e. the connector file in this case.

Step 4) Problem solved & connection created

huangapple
  • 本文由 发表于 2020年10月7日 17:43:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定