很抱歉我的问题,但我自己无法解决。在这里阅读不同的主题并尝试阅读教学法...
我有MySQL服务器v8,并尝试在Java中启动简单示例以从服务器获取数据。
我将classpat设置为系统变量,将mysql-connector-java-8.0.13.jar放在此处。 comman echo%CLASSPATH%正确显示它。我也将连接器放在所有项目文件夹xD中(也无济于事)。
import java.sql.*;
public class file_test {
// JDBC URL, username and password of MySQL server
private static final String url = "jdbc:mysql://localhost:3306/MySQL80";
private static final String user = "root";
private static final String password = "123";
// JDBC variables for opening and managing connection
private static Connection con;
private static Statement stmt;
private static ResultSet rs;
public static void main(String args[]) {
String query = "select count(*) from actor";
try {
// opening database connection to MySQL server
con = DriverManager.getConnection(url, user, password);
// getting Statement object to execute query
stmt = con.createStatement();
// executing SELECT query
rs = stmt.executeQuery(query);
while (rs.next()) {
int count = rs.getInt(1);
System.out.println("Total number of actors in the table : " + count);
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
} finally {
//close connection ,stmt and resultset here
try {
con.close();
} catch (SQLException se) { /*can't do anything */ }
try {
stmt.close();
} catch (SQLException se) { /*can't do anything */ }
try {
rs.close();
} catch (SQLException se) { /*can't do anything */ }
}
}
}
问题是我尝试启动节目时,IJ说:
"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA\lib\idea_rt.jar=63236:C:\Program Files\JetBrains\IntelliJ IDEA\bin" -Dfile.encoding=UTF-8 -classpath F:\work\program\file_test\out\production\file_test file_test
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/MySQL80
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at file_test.main(file_test.java:21)
Exception in thread "main" java.lang.NullPointerException
at file_test.main(file_test.java:38)
Process finished with exit code 1
我想从IJ运行此代码并因此从DB获取数据。