AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-jdbc: Explain the Transaction concept with the help of JDBC and jsp?

Transaction concept like bank transactions.

The bank transaction operations like

1) create account
2) deposit amount
3) withdraw amount
4) check balance
5) delete account

All the data store in oracle database.


Explain the concept as early as possible with example & sample code.

corejava x 351
jdbc x 32
Posted On : 2016-09-06 08:25:48.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
up-rate
4
down-rate

Answers


Transactions comes in the picture when we perform update or insert operations. Suppose in case of cash amount withdraw we need to update the account balance as current balance - withdraw amount. Now ATM gives cash only if update operation executes successfully. otherwise it´ll roll back the transaction means no amount will be deducted. or suppose we are transferring some amount from one account to another, so in this case for first account we have to update substracted amount and in second account we need to update additional deposit amount. Now consider the situation we have updated the second account with additional amount and while updating first account with withdraw amount something went wrong, In this case we have to roll back whole transaction means the amount which we have credited in second account should be rolled back. By default autoCommit flag is true, so if we want to use transactions in our project we have to set it as false, first perform all the operation and if everything goes fine means there is no exception then commit it.


Connection connection=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
connection.setAutoCommit(false);
PreparedStatement ps =connection.prepareStatement("insert into student values (?,?,?,?)");
ps.setInt(1,Integer.parseInt(request.getParameter("sid")));
ps.setString(2,request.getParameter("sname"));
ps.setString(3,request.getParameter("adrs"));
ps.setString(4,request.getParameter("phone"));
if(ps.executeUpdate()>0){
connection.commit();
out.print("Registered Successfully");
}else{
connection.rollback();
out.print("Something Went Wrong!");
}
}catch (Exception e) {
connection.rollback();
e.printStackTrace();
}
finally {
if(connection != null) {
connection.close();
}
}

Posted On : 2016-09-06 14:47:46
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523187021414
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in