Im doing a webscaping project in Java and im having trouble with executing the SQL code to load the variables. im using IntelliJ with maven as the build.
I keep getting this error
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)
this is my code for the scraper 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();
}
}
}
in my error it says line 54 which is
rs = stmt.executeQuery();
I just dont know what im missing here... Appreciate any Geniuses out there to help.
CodePudding user response:
executeQuery()
is meant for fetch (SELECT
) statements that return results.
INSERT
(as well as UPDATE
and DELETE
) statements should use executeUpdate()
.