尝试连接sql数据库和Java代码,但出现找不到驱动程序的错误。我已将连接器下载到相应的文件夹中,但找不到问题
我尝试跑步: java -cp。; C:\ asd \ mysql-connector-java-8.0.16 \ mysql-connector-java-8.0.16.jar JdbcSelectTest
通过命令行,但它给了我错误
import java.sql.*; // Using 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
public class JdbcSelectTest { // Save as "JdbcSelectTest.java"
public static void main(String[] args) {
try (
// Step 1: Allocate a database 'Connection' object
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ebookshop?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
"myuser", "xxxx"); // For MySQL only
// The format is: "jdbc:mysql://hostname:port/databaseName", "username", "password"
// Step 2: Allocate a 'Statement' object in the Connection
Statement stmt = conn.createStatement();
) {
// Step 3: Execute a SQL SELECT query. The query result is returned in a 'ResultSet' object.
String strSelect = "select title, price, qty from books";
System.out.println("The SQL statement is: " + strSelect + "\n"); // Echo For debugging
ResultSet rset = stmt.executeQuery(strSelect);
// Step 4: Process the ResultSet by scrolling the cursor forward via next().
// For each row, retrieve the contents of the cells with getXxx(columnName).
System.out.println("The records selected are:");
int rowCount = 0;
while(rset.next()) { // Move the cursor to the next row, return false if no more row
String title = rset.getString("title");
double price = rset.getDouble("price");
int qty = rset.getInt("qty");
System.out.println(title + ", " + price + ", " + qty);
++rowCount;
}
System.out.println("Total number of records = " + rowCount);
} catch(SQLException ex) {
ex.printStackTrace();
} // Step 5: Close conn and stmt - Done automatically by try-with-resources (JDK 7)
我期望数据库能够连接,感谢您的帮助!我认为该错误与第8行的输入有关
答案 0 :(得分:-2)
您必须在库中包含Driver jar。我正在使用此“ mysql-connector-java-8.0.15.jar”。
参考Where can I download mysql jdbc jar from?
https://repo1.maven.org/maven2/mysql/mysql-connector-java/
将文件放入您的类路径。