The ResultSet can not be null for Statement.executeQuery

628 Views Asked by At

I am inserting data to a MySQL DB, but get an error, when executing the insert statement:

The ResultSet can not be null for Statement.executeQuery.

Although if i check the database table, I find that the values have been successfully inserted. This is confusing.

Code Snippet

Connection connection = null;
connection = DriverManager.getConnection("jdbc:google:rdbms://name-of-instance");
Statement check = connection.createStatement();
java.sql.ResultSet resultset = null;
resultset = check.executeQuery("insert into table_name values (values-inserted-here);");
2

There are 2 best solutions below

0
On

To execute queries that update, delete or insert any data in your DB, you cannot use executeQuery(String sql) (check here the docs), but executeUpdate(String sql) instead.

So instead of:

check.executeQuery("insert into table_name values (values-inserted-here);");

You should do:

check.executeUpdate("insert into table_name values (values-inserted-here);");

The ResultSet is always null in an insert, delete or update, that is why it was giving you that error. If you use executeUpdate the error should be gone.

Hope it helps!

0
On

No need to use ResultSet. ResultSet only used when you fetch data from query like select query. Just use

Statement check = connection.createStatement();
check.executeUpdate("insert into table_name values (values-inserted-here)");

and for tutorial use this link