Use a transaction instance for JDBC

Source: Internet
Author: User
Packageqddx. JDBC; importjava. SQL. *; publicclassuseTransaction {publicstaticvoidmain (String [] args) {TODOAuto-example; Statementstnull; PreparedStatementpstnull; ResultSetrs

Package qddx. JDBC; import java. SQL. *; public class useTransaction {public static void main (String [] args) {// TODO Auto-generated method stub Connection conn = null; Statement st = null; PreparedStatement pst = null; resultSet rs

Package qddx. JDBC; import java. SQL. *; public class useTransaction {public static void main (String [] args) {// TODO Auto-generated method stub Connection conn = null; Statement st = null; PreparedStatement pst = null; resultSet rs = null; Savepoint sp = null; try {conn = JDBC_Connection.getConnection (); // specify the transaction isolation level conn. setTransactionIsolation (conn. TRANSACTION_READ_UNCOMMITTED); pst = conn. prepareStatement ("create table users (id smallint, username text)"); pst.exe cute (); // submit the transaction conn. commit (); pst. close ();} catch (SQLException e) {System. err. println ("failed to connect to database or create table"); System. err. println ("transaction rollback to rollback point"); try {conn. rollback ();} catch (SQLException ex) {// ex. printStackTrace (); System. out. println ("failed rollback");} try {conn. setSavepoint (); // set a storage point st = conn. createStatement (); st.exe cuteUpdate ("insert into users values (110, 'janes ')"); // execute the update statement // st.exe cuteUpdate ("insert into users values ('shibai', 'janes ')"); // example of conn that fails to execute the update statement. commit (); // submit the transaction conn. releaseSavepoint (sp); // release the storage point st. close (); conn. close ();} catch (SQLException et) {System. err. println ("operation failed"); System. err. println ("transaction rollback to storage point"); try {conn. rollback (sp); st. close (); conn. close ();} catch (SQLException exc) {System. out. println ("failed to roll back to the storage point"); // exc. printStackTrace ();} // et. printStackTrace ();} // e. printStackTrace ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.