简体   繁体   English

PostgreSQL:事务

[英]PostgreSQL: Transactions

I am writing a client in JAVA using JDBC. 我正在使用JDBC在JAVA中编写客户端。 I want to execute a statement like so 我想执行这样的语句

resultSet = statement.executeQuery("BEGIN; Delete from Table id=1 Delete from table2 id=1 COMMIT;");

Obviously the above statemnt will not work. 显然,以上状态将不起作用。 So can I proceede as follows? 那么我可以进行以下操作吗? Instead I am using Insert in this example. 相反,我在此示例中使用插入。

con.setAutoComitt(false); 

String qry1 = "..." 
pst1 = con.prepareStatement(qry1) 
//Insert code here to add values to prepared statement pst1 
pst1.executequery(); 

String qry2 = "..." 
pst2 = con.prepareStatement(qry2) 
//Insert code here to add values to prepared statement pst2 
pst2.executequery(); 

con.comitt(); 

Would this be correct? 这是正确的吗? Any help or docs would be great. 任何帮助或文档都很好。 I could not find anything. 我什么都找不到。

I mostly do the same when i want to communicate with my postgreSQL database 当我想与我的postgreSQL数据库通信时,我通常会做同样的事情

stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select osm_id from vertices;");

you have to execute every command in different "statement" cause each statement returns results for one query only if the statement contains more than one command it will throw an exception 您必须在不同的“语句”中执行每个命令,因为每个语句仅在该语句包含多个命令时才返回一个查询的结果,否则将引发异常

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

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