开发者

How to write a transaction using jdbc driver?

开发者 https://www.devze.com 2023-03-18 14:14 出处:网络
I wan开发者_开发百科t to write a transaction using jdbc in java. I have tried this simple transaction

I wan开发者_开发百科t to write a transaction using jdbc in java.

I have tried this simple transaction

"BEGIN TRANSACTION"+NL+"GO"+NL+"UPDATE table SET col='test' where id=1010"+NL+"GO"+NL+"COMMIT"

I have tried with

NL= "\n" and NL="\r\n" and NL="\r"

but I get always the following error:

java.sql.SQLException: Incorrect syntax near 'GO'.

In sql server management studio the transaction works


Get your Connection object. Turn off auto commit.

connection.setAutoCommit(false);

Wrap your entire transaction in a try-catch block. When you finish processing your inserts/updates, call:

connection.commit();

If you get an exception, call:

connection.rollback();

Don't put the transaction statements in your JDBC's SQL at that point. I suggest looking at wrappers, such as Hibernate and JPA. Transactions in JDBC can get pretty long winded.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号