简体   繁体   English

PostgreSQL的Java JDBC:使用添加列查询执行的PreparedStatement返回false

[英]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) 查询类似于(有效的SQL)

ALTER TABLE mytable ADD COLUMN mycolumn datatypeinfo

Anyway, it executes fine and the column is created. 无论如何,它执行得很好,并且创建了列。 But the execute statement returns false. 但是execute语句返回false。 I tried with executeUpdate and that returns 0 rows. 我尝试了executeUpdate,它返回0行。

Note that this whole thing is within a transaction with con.setAutoCommit set to false. 请注意,整个过程都在con.setAutoCommit设置为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. 因此它必须在事务中运行。

The return value of execute() does not indicate whether it was successful, but if the query that was run returned a result. execute()的返回值并不指示它是否成功,而是如果运行的查询返回了结果。

As the ALTER TABLE does not return a result, the execute() rightfully returns false 由于ALTER TABLE不返回结果,因此execute()正确返回false

Quote from the Javadocs : 引用Javadocs

Returns: true if the first result is a ResultSet object; 返回: 如果第一个结果是ResultSet对象,则 返回 true; 否则 返回 true false if the first result is an update count or there is no result 如果第一个结果是更新计数或没有结果,则返回 false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM