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();

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap