JDBC Coding
JDBC Coding
CODING:
import javax.swing.JOptionPane;
import java.sql.*;
public class Main{
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;
}
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;
}
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)
{
}
}
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)
{
}
}
catch(Exception e){}
}
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)
{
}
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