Java连接MySQL报错:com.mysql.jdbc.Driver类找不到求助
解决MySQL连接的ClassNotFoundException及代码问题
Hey there, let's break down and fix your issues one by one. The core error you're seeing (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver) is about missing MySQL JDBC driver, but there are also a few code issues we need to address to make your query work properly.
1. Fix the Missing MySQL JDBC Driver Issue
The com.mysql.jdbc.Driver class belongs to MySQL 5.x JDBC driver. If you're using MySQL 8.x, the driver class name has changed to com.mysql.cj.jdbc.Driver, and you need the corresponding driver jar. Here's how to resolve this:
- If using Maven/Gradle: Add the correct dependency to your build file.
For MySQL 8.x (recommended):
For MySQL 5.x:<!-- Maven --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> - If manually adding the jar: Download the appropriate driver jar from MySQL's official site, then add it to your project's build path (in IDEs like Eclipse/IntelliJ, right-click project -> Build Path -> Add External Archives).
2. Fix Code Issues in Your Java Program
Even after fixing the driver, your code has a couple of bugs that will prevent it from working:
- Table name with hyphen: Your table
lottery1955-00contains a hyphen, which MySQL interprets as a subtraction operator. You need to wrap the table name in backticks (`) in your SQL query. - ResultSet navigation: You must call
rs.next()before accessing data from the ResultSet, as the cursor starts before the first row. - Resource management: Use try-with-resources to automatically close connections, statements, and result sets to avoid resource leaks.
- Typo check: I noticed an extra tab in your original username field—make sure your credentials have no accidental spaces or tabs.
Corrected Code
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ExampleForDB { public static void main(String[] args) { // Use the correct driver class based on your MySQL version String driverClass = "com.mysql.cj.jdbc.Driver"; // For MySQL 8.x // String driverClass = "com.mysql.jdbc.Driver"; // Uncomment this for MySQL 5.x String url = "jdbc:mysql://localhost:3306/******?useSSL=false&serverTimezone=UTC"; // Add timezone for MySQL 8.x String username = "*****"; String password = "******"; // Try-with-resources auto-closes all resources try { Class.forName(driverClass); try (Connection con = DriverManager.getConnection(url, username, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select Number1 from `lottery1955-00` where id=1")) { System.out.println("Successful connection with MySQL!"); // Move cursor to the first row if (rs.next()) { System.out.println(rs.getInt("Number1") + " " + rs.getString("Number1")); } else { System.out.println("No record found with id=1"); } } } catch (ClassNotFoundException e) { System.err.println("Failed to load JDBC driver: " + e.getMessage()); e.printStackTrace(); } catch (SQLException e) { System.err.println("Database error occurred: " + e.getMessage()); e.printStackTrace(); } } }
3. Additional Notes
- For MySQL 8.x, you need to add
serverTimezoneparameter to your JDBC URL (likeserverTimezone=UTC) to avoid timezone-related errors. - Double-check your database name, username, and password in the
getConnectioncall—ensure there are no extra spaces or typos.
内容的提问来源于stack exchange,提问作者Patrahi
相关产品推荐
相关产品推荐

