You are on page 1of 7

JDBC- BANK TRANSACTION

CODING:

import javax.swing.JOptionPane;
import java.sql.*;
public class Main{

static String url = "jdbc:odbc:Temp";


//syntax: String url = jdbc:mySubprotocol:myDataSource";
static Statement stmt;
static Connection con;
public static void main(String args[])
{

JOptionPane.showMessageDialog(null,
"Welcome To Bank Transaction Using JDBC");
int choice = -1;

try
{
do{
choice = getChoice();
if (choice != 0)
getSelected(choice);
}
while ( choice != 0);
System.exit(0);
}
catch(Exception e)
{
}

}
public static int getChoice()
{
String choice;
int ch;
choice = JOptionPane.showInputDialog(null,
"1. Deposit an Amount\n"+
"2. Withdraw an Amount\n"+
"3. Find Balance of an account\n"+
"4. Find Balance of an customer\n"+
"5. Find Details of Customers in a Branch\n"+
"6. Find Total Amount at Each Branch\n"+
"0. Exit\n\n"+
"Enter your choice");
ch = Integer.parseInt(choice);
return ch;

public static void getSelected(int choice){


if(choice==1)deposit();
if(choice==2)withdraw();
if(choice==3)acbal();
if(choice==4)cbal();
if(choice==5)acprint();
if(choice==6)totalamt();
}

public static Connection getConnection()


{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//syntax: Class.forName("myDriver.ClassName");

}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url);// userid, password);

}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}

return con;
}

public static void deposit()


{
Connection con = getConnection();
String in,acc, createString;
int i,j=0,ac;
acc=(JOptionPane.showInputDialog(null,"Enter Account number:"));
try
{
PreparedStatement ps = con.prepareStatement(
"select bal from Account where accno IN"
+"(select acno from Depositor where acno=(?))");
ps.setString(1,acc);
ResultSet result=ps.executeQuery();

if(result.next())
{
j=result.getInt("bal");
in= JOptionPane.showInputDialog(null,"Enter Deposit amount:");
i=Integer.parseInt(in);
i=i+j;
ps = con.prepareStatement(
"update Account set bal=(?) where ccno=(?)");
ps.setInt(1,i);
ps.setString(2,acc);
ps.executeUpdate();
JOptionPane.showMessageDialog(null,
"Deposited Successfully!\nYour Current Balance is:"+i);
}
else
JOptionPane.showMessageDialog(null,"Account not found");
stmt.close();
con.close();

}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
catch(Exception e)
{
}
}

public static void withdraw()


{
Connection con = getConnection();
String in,acc;
int i,j=0,ac;
String createString;
acc=(JOptionPane.showInputDialog(null,"Enter Account number:"));

try
{
PreparedStatement ps = con.prepareStatement(
"select bal from Account where accno IN"
+"(select acno from Depositor where acno=(?))");
ps.setString(1,acc);
ResultSet result=ps.executeQuery();

if(result.next())
{

j=result.getInt("bal");
in= JOptionPane.showInputDialog(null,"Enter Withdraw amount:");
i=Integer.parseInt(in);
if(i<j)
{
i=j-i;
ps = con.prepareStatement(
"update Account set bal=(?) where accno=(?)");
ps.setInt(1,i);
ps.setString(2,acc);
ps.executeUpdate();
JOptionPane.showMessageDialog(null,
"Withdrawn Successfully !\nYour Current Balance :"+i);
}
else
JOptionPane.showMessageDialog(null,
"TRANSACTION NOT POSSIBLE !CURRENT BALANCE IS:"+j);

}
else
JOptionPane.showMessageDialog(null,"Account not found");
stmt.close();
con.close();

}
catch(Exception e)
{
}

public static void cbal() {

Connection con = getConnection();


String in,cname, print;
int i,j=0,ac;
cname=(JOptionPane.showInputDialog(null,"Enter Customer Name:"));
try
{
PreparedStatement ps = con.prepareStatement(
"select * from Account where accno IN("
+"select acno from Depositor where cname=(?))");
ps.setString(1,cname);
ResultSet result=ps.executeQuery();
print="---------------------------------------------------------\n";
print+="Cname\tAccno\tBranch Name\tBalance\n";
print+="---------------------------------------------------------\n";
if(result.next())
print+=cname+"\t"+result.getString("accno")+"\t"+result.getString("bname")
+"\t"+result.getString("bal");
print+="\n---------------------------------------------------------\n";
System.out.println(print);

}
catch(Exception e){}

public static void acbal()

Connection con = getConnection();


String in,acc, print;
int i,j=0,ac;
acc=(JOptionPane.showInputDialog(null,"Enter Account number:"));
try
{
PreparedStatement ps = con.prepareStatement(
"select * from Account where accno IN("
+"select acno from Depositor where acno=(?))");
ps.setString(1,acc);
ResultSet result=ps.executeQuery();
print="---------------------------------------------------------\n";
print+="Accno\t\tBname\t\tBalance\n";
print+="---------------------------------------------------------\n";
if(result.next())
print+=result.getString("accno")+"\t\t"+result.getString("bname")+"\t"
+result.getString("bal");
print+="\n---------------------------------------------------------\n";
System.out.println(print);

}
catch(Exception e){}
}
public static void acprint(){
Connection con = getConnection();
String bname,acno,cname,print;
bname=JOptionPane.showInputDialog(null,"Enter Branch name:");
try
{
PreparedStatement ps = con.prepareStatement(
"select * from Customer where cname IN ("
+"select cname from Depositor where acno IN ("
+"select accno from Account where bname=(?)))");
ps.setString(1,bname);
ResultSet result=ps.executeQuery();

print="---------------------------------------------------------\n";
print+="Name\t\tStreet\t\tCity\n";
print+="---------------------------------------------------------\n";
while(result.next() )
print+=result.getString("cname")+"\t"+result.getString("street")+"\t"
+result.getString("city")+"\n";
print+="---------------------------------------------------------";
System.out.println(print);
stmt.close();
con.close();

}
catch(Exception e)
{
}

public static void totalamt() {

Connection con = getConnection();


String bname,print;
ResultSet result,b;

try {
stmt = con.createStatement();
b=stmt.executeQuery("select distinct(bname) from Account");

print="---------------------------------------------------------\n";
print+="Branch Name\t\tTotal Amount\n";
print+="---------------------------------------------------------\n";
while(b.next())
{
PreparedStatement ps = con.prepareStatement(
"select sum(bal) from Account where bname =(?) ");
bname=b.getString("bname");
ps.setString(1,bname);
result=ps.executeQuery();
while(result.next() )
print+=bname+"\t\t"+result.getString(1)+"\n";
}
print+="---------------------------------------------------------";
System.out.println(print);
stmt.close();
con.close();

}
catch(Exception e)
{
}

}
}//End of class

You might also like