Followers

JDBC: Transaction in JDBC


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class AutoCommitFalse {
    public static void main(String[] args) throws Exception
    {
        Connection con = null;
        Statement stmt = null;
     
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            String url = "jdbc:derby://localhost:1527/hari";
            con = DriverManager.getConnection(url, "app","app");
            System.out.println(con.getAutoCommit());          
            con.setAutoCommit(false);
            System.out.println(con.getAutoCommit());
            stmt = con.createStatement();
            stmt.executeUpdate("insert into MYEMP values(901,'Mohan1',8001,'USA')");
            stmt.executeUpdate("insert into MYEMP values(902,'Mohan1',8001,'USA')");
            stmt.executeUpdate("insert into MYEMP values(903,'Mohan1',8001,'USA')");
            con.commit();
            stmt.executeUpdate("insert into MYEMP values(904,'Mohan1',8001,'USA')");
            con.rollback();
            stmt.executeUpdate("insert into MYEMP values(907,'Mohan1',8001,'USA')");
            con.commit();          
    }
}

No comments:

Post a Comment