使用 JDBC 执行 SQL 查询时出现问题。结果集出错
Trouble with SQL query execution using JDBC. getting error with result set
我在 Java 中做一个 webscaping 项目,我在执行 SQL 代码加载变量时遇到了问题。我使用 IntelliJ 和 maven 作为构建。
我一直收到这个错误
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)
这是我的爬虫代码class
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();
我只是不知道我在这里缺少什么...
感谢任何天才的帮助。
executeQuery()
用于获取 return 结果的 (SELECT
) 语句。
INSERT
(以及 UPDATE
和 DELETE
)语句应使用 executeUpdate()
.
我在 Java 中做一个 webscaping 项目,我在执行 SQL 代码加载变量时遇到了问题。我使用 IntelliJ 和 maven 作为构建。
我一直收到这个错误
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)
这是我的爬虫代码class
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();
我只是不知道我在这里缺少什么... 感谢任何天才的帮助。
executeQuery()
用于获取 return 结果的 (SELECT
) 语句。
INSERT
(以及 UPDATE
和 DELETE
)语句应使用 executeUpdate()
.