简体   繁体   中英

PostgreSQL: Transactions

I am writing a client in JAVA using JDBC. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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