Krishna
Krishna

Reputation: 361

Java JDBC with PostgreSQL: PreparedStatement Execute with Add Column Query returns false

I am trying to add a column to a database table and

PreparedStatement  ps = con.prepareStatement(query);  
ps.execute();
ps.close();

query is something like (valid SQL)

ALTER TABLE mytable ADD COLUMN mycolumn datatypeinfo

Anyway, it executes fine and the column is created. But the execute statement returns false. I tried with executeUpdate and that returns 0 rows.

Note that this whole thing is within a transaction with con.setAutoCommit set to false. So I am not sure if that is the problem. The issue is that I have to create the column before I can go ahead with other update queries. So it has to run in a transaction.

Upvotes: 0

Views: 2289

Answers (1)

user330315
user330315

Reputation:

The return value of execute() does not indicate whether it was successful, but if the query that was run returned a result.

As the ALTER TABLE does not return a result, the execute() rightfully returns false

Quote from the Javadocs:

Returns: true if the first result is a ResultSet object; false if the first result is an update count or there is no result

Upvotes: 5

Related Questions