JDBC SQLException Statement.executeQuery() cannot issue statements that do not produce result sets


Post Banner
Code:
String insertQuery ="insert into students values(?,?,?,?)";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.executeQuery();


Stacktrace:
Exception in thread "main" 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.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:928)
 at JDBCInsertExample.main(JDBCInsertExample.java:17)


Issue:

You get SQLException when you try to call executeQuery() on PreparedStatement Object if it does not return a result set, for example, INSERT, UPDATE or DELETE queries.



Solution:

Replace preparedStatement.executeQuery(); with preparedStatement.execute();

-


Have Questions? Post them here!

Top Hashtags:

Advertisements

Advertisements




Advertisements