0

I am trying to insert into a db that I have, and I'd like to do so through parameters. I am connecting to a postgres db using java.

I can connect to the db just fine. I know that because I have various operations that I am using that are already working were I can see, and update existing rows in my db. I am having trouble with INSERT.

I have the following:

private String _update_rentals = "INSERT into rentals (cid, mid) values (?,?)"; 
private PreparedStatement _update_rentals_statement; 

private String _update_movie_status = "UPDATE Movie SET checkedout = true WHERE mid = ?";
private PreparedStatement _update_movie_status_statement;

And I initialize them:

_update_movie_status_statement = _customer_db.prepareStatement(_update_movie_status); 
        _update_rentals_statement = _customer_db.prepareStatement(_update_rentals); 

And

while (movieAvail.next()){
            System.out.println(movieAvail.getBoolean(1));
            if (movieAvail.getBoolean(1) == false){
                //Do chekcout

                _update_rentals_statement.clearParameters();
                _update_rentals_statement.setInt(1, cid);
                _update_rentals_statement.setInt(2, mid);
                _update_rentals_statement.executeQuery(); 

                _update_movie_status_statement.clearParameters();
                _update_movie_status_statement.setInt(1, mid);
                _update_movie_status_statement.executeQuery(); 


                System.out.println("Enjoy your movie!"); 
            } 
}

I am getting an error with both of the executeQuery() calls. For some reason I am getting the following error with both:

Exception in thread "main" org.postgresql.util.PSQLException: No results were returned by the query.

I looked at other posts, and I believed that I was following syntax for both insert/ update correctly, so maybe I am overlooking some aspect of this.

This is all part of a larger code base, so I did not want to include the methods these pieces of code are in. But these are the isolated instances which play a part with this code.

3
  • I'm assuming you actually created the PreparedStatement instances? Commented Nov 16, 2015 at 22:24
  • Yes, Sorry I will make that clearer in the description. Commented Nov 16, 2015 at 22:25
  • you need to use executeUpdate() not executeQuery() Commented Nov 16, 2015 at 22:34

2 Answers 2

3

In general, when you execute a query, you are willing to retrieve some kind of information from the database. This is usually the case when you are executing SELECT queries. However, with INSERT and UPDATE statements, you are not querying the database, you are simply executing an update or inserting new rows. In the documentation of PreparedStatement you can see in which cases an exception is being thrown when you try to call executeQuery:

Throws: SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet object

So in your case the problem is that your statements do not return a ResultSet. You should use execute or executeUpdate instead. The former simply executes the update, while the latter does the same, but also returns the number of affected rows.

Sign up to request clarification or add additional context in comments.

Comments

1

I think the main issue is that you are calling executeQuery(), which expects a result to be returned, but Insert/Update are not queries and don't return a result. Try just calling execute().

1 Comment

This was exactly it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.