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

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

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)


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.


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

Have Questions? Post them here!
Try Out Code2care Dev Tools:


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.