Transation demo

package op.controls;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

 

import op.db.ConnectionManager;

 

public class transSql{

   

    public boolean trans(String sqlupdate,String sqlinsert){

        boolean objbl=false;

            Connection con=ConnectionManager.getConnection();

            PreparedStatement pstmtupdate=null;

            PreparedStatement pstmtinsert=null;

            try{

                con.setAutoCommit(false);              

                //pstmtupdate.executeUpdate(sqlupdate);

                pstmtupdate=con.prepareStatement(sqlupdate);

                pstmtupdate.executeUpdate();

               

                pstmtinsert=con.prepareStatement(sqlinsert);

                pstmtinsert.executeUpdate();

               

                con.commit();

                objbl=true;

               

            }catch(Exception e){

                e.printStackTrace();

            }

        return objbl;

    }

}

你可能感兴趣的:(java,sql)