Java

Banking Transaction Management in Java Mysql

in this tutorial will teach you Bank account to account transaction using java The set of one or more sql statements that are executed as a single unit called as transaction.the example is very useful for learning transaction jdbc.the main purpose of writing transaction if any query fails the entire transaction is rolled back.the good example is used to transfer the money from one bank account to another.if the transaction is competed the money is deducted from the first account and added to the second account.while doing the transaction error occurs both account remain unchanged. if the both query is working correctly only transaction commit other wise rollback.

Establish the Database Connection.

In this example created three different PreparedStatements

PreparedStatement pst;

PreparedStatement pst1;

PreparedStatement pst2;

public bank() {
        initComponents();
        
        Connect();
    }
    
    Connection con;
    PreparedStatement pst;
    PreparedStatement pst1;
    PreparedStatement pst2;
    ResultSet rs;
    
    public void Connect()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/sambank", "root", "");
        } catch (ClassNotFoundException ex) {
           ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
   
    }

Paste the Code Inside the Transfer Button

try {
            String acc1 = txtacc1.getText();
            String acc2 = txtacc2.getText();
            String Account = txtamount.getText();

          Statement st1=con.createStatement( );
          
          con.setAutoCommit(false);
          
            //sender
            String SQL1="update accountholder set balance = balance-? where acc_id = ?";
            pst = con.prepareStatement(SQL1);
            pst.setString(1, Account);
            pst.setString(2, acc1);
            pst.executeUpdate();

            
            //receiver
            String SQL2="update accountholder set balance = balance+? where acc_id = ?";
            pst1 = con.prepareStatement(SQL2);
            pst1.setString(1, Account);
            pst1.setString(2, acc2);
            pst1.executeUpdate();
            
            //transer
            String SQL3="insert into transfer(f_account,to_account,amount)values(?,?,?)";
            pst2 = con.prepareStatement(SQL3);
            pst2.setString(1, acc1);
            pst2.setString(2, acc2);
            pst2.setString(3, Account);
            pst2.executeUpdate();
                
                rs.next();
                
                if(rs.getFloat(1)<0)
                {
                    con.rollback();
                    JOptionPane.showMessageDialog(this, "Transaction Failed");
                }
                else
                {
                     con.commit();
                    JOptionPane.showMessageDialog(this, "Transaction Success");
                }   

             Clear();
  
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex);
        }

 

I have attached the video tutorial below it will help you  to do this  step by step.

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

6 days ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

1 week ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 week ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

2 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

4 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

4 weeks ago