我在 Java 中做一個網頁美化專案,并且在執行 SQL 代碼來加載變數時遇到了麻煩。我使用帶有 maven 的 IntelliJ 作為構建。
我不斷收到此錯誤
java.sql.SQLException: Statement.executeQuery() cannot issue statements that do not produce result sets.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1135)
at com.cst3511.website.scraper.Scarper.main(Scarper.java:54)
這是我的刮板類代碼
public class Scarper {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
final String url =
"https://www.myshiptracking.com/ports-arrivals-departures/?mmsi=&pid=GB&type=0&time=&pp=50";
try {
Document doc = Jsoup.connect(url).get();
Element table = doc.select(".cs-table").first();
Elements rows = doc.select("div.table-row");
for (Element row : rows) {
String event = row.select("div.col:nth-of-type(2)").text();
String time = row.select("div.col:nth-of-type(3)").text();
String port = row.select("div.col:nth-of-type(4)").text();
String vessel = row.select(".td_vesseltype.col").text();
// connecting to SQL
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/oceana",
"user", "pass");
ResultSet rs;
PreparedStatement stmt = conn.prepareStatement("INSERT INTO status_table(status, time, port , vessel) VALUES (?,?,?,?)");
stmt.setString(1, event);
stmt.setString(2, time);
stmt.setString(3, port);
stmt.setString(4, vessel);
rs = stmt.executeQuery();
if (rs.next()){
System.out.println(rs.getString(1) "" rs.getString(2) " " rs.getString(3) "" rs.getString(4));
}
}
} catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace();
}
}
}
在我的錯誤中,它說第 54 行是
rs = stmt.executeQuery();
我只是不知道我在這里缺少什么...感謝那里的任何天才提供幫助。
uj5u.com熱心網友回復:
executeQuery()用于回傳結果的 fetch ( SELECT) 陳述句。
INSERT(以及UPDATE和DELETE)陳述句應該使用executeUpdate().
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431922.html
