ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Transaction Management Tool
package com.nt.batch;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.Statement;
importjava.util.Scanner;
publicclassTxMgmtDemo{
publicstaticvoidmain(String[] args) throwsException{
Connectioncon=null;
try{
//take inputfromuser
Scannersc=newScanner(System.in);
System.out.println("EnterSource Accountno:");
intsrcacno=sc.nextInt();
System.out.println("EnterDestination Accountno:");
intdestno=sc.nextInt();
System.out.println("Enterthe amounttodeposit:");
longamount=sc.nextInt();
//registerdriverandcreate connection
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","password");
//disable AutoCommitfirst....
con.setAutoCommit(false);//now commitof datainside database will be inourcontrol...
//create Statementobj
Statementst=con.createStatement();
//create batchprocess....
//towithdrawmoney
st.addBatch("update bank_accountsetbalance=balance-"+amount+"where
accno="+srcacno);
//fordepositmoney
st.addBatch("update bank_accountsetbalance=balance+"+amount+"where accno="+destno);
//execute BatchProcess
int[] result=st.executeBatch();
//processtransaction
booleanflag=true;
//processthe result...
for(inti=0;i<result.length;++i){
if(result[i]==0){
flag=false;
break;
}
}//for
//commitor rollbackthe txMgmt
if(flag==false){
con.rollback();
System.out.println("Moneynottransferred...");
}
else{
con.commit();
System.out.println("Moneytransferred...");
}//if
//close jdbcconnections
st.close();
con.close();
}//try
catch(Exception e){
try{con.rollback();}
catch(Exceptione1){e1.printStackTrace();}
}//catch
}//main
}//class

More Related Content

Transaction Management Tool