You are on page 1of 18

import import import import import import

java.sql.*; java.awt.*; java.awt.event.*; javax.swing.*; javax.swing.event.*; java.util.regex.*;

public class BManage extends JFrame { JMenuBar mb; JMenu A,T; JLabel l,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14; JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14; JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12; String Name; int Balance; boolean flag1=false,flag2=false,flag3=false,flag4=false,sign=false; public BManage() { JFrame jf=new JFrame(); setLayout(new BorderLayout()); setSize(700,700); setIconImage(Toolkit.getDefaultToolkit().getImage("A.jpg")); l=new JLabel(new ImageIcon("main.jpg")); mb = new JMenuBar(); A = new JMenu("*ACCOUNT*"); JMenuItem o = new JMenuItem("Open Account"); JMenuItem c = new JMenuItem("Close Account"); JMenuItem e = new JMenuItem("Exit"); o.addActionListener(new Open()); A.add(o); c.addActionListener(new Close()); A.add(c); e.addActionListener(new Exit()); A.add(e); T = new JMenu("*TRANJACTION*"); JMenuItem d = new JMenuItem("DEPOSIT"); JMenuItem w = new JMenuItem("WITHDRAW"); d.addActionListener(new Deposit()); T.add(d); w.addActionListener(new Withdraw()); T.add(w); mb.add(A); mb.add(T); setJMenuBar(mb); add(l); addWindowListener(new mylistener4()); setTitle("Bank Simulator"); setVisible(true); } public class Dbinfo { Connection con;

Statement stmt; PreparedStatement ps; ResultSet rs; { try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/bankdata?user=root&password=root"; con=DriverManager.getConnection(url); stmt=con.createStatement(); System.out.println("driver loaded"); System.out.println("connection created....."); } catch(Exception e) { e.printStackTrace(); System.out.println("error in connection(Dbinfo)"); } } { try { ps=con.prepareStatement("insert into account(Name,Balance) value s(?,?)"); ps.setString(1,Name); ps.setInt(2,Balance); ps.executeUpdate(); System.out.println("record inserted successfully"); JOptionPane.showMessageDialog(null,"Account opened successfully" ,"Account opened successfully",JOptionPane.INFORMATION_MESSAGE); sign=true; ShowAcc(sign); } catch(Exception e) { e.printStackTrace(); } } public void closeDl(String acn) { try { t5.setText(""); t6.setText(""); rs=stmt.executeQuery("Select Name,Balance from a ccount where AccNo='"+acn+"'"); while(rs.next()) { t5.setText(rs.getString(1)); t6.setText(rs.getString(2)); } if(t5.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"no match found","error occ public void insert(String Name,int Balance)

ured",JOptionPane.ERROR_MESSAGE); } } catch(Exception e) { System.out.println("error in close"); } } public void Delete(String acn) { try { t5.setText(""); t6.setText(""); rs=stmt.executeQuery("Select Name,Balance from account where Acc No='"+acn+"'"); while(rs.next()) { t5.setText(rs.getString(1)); t6.setText(rs.getString(2)); } if(t5.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"Account not found","error occured",JOptionPane.ERROR_MESSAGE); } else { ps=con.prepareStatement("Delete From account where AccNo='"+acn+" '"); ps.executeUpdate(); JOptionPane.showMessageDialog(null,"Account Deleted","Account Del eted",JOptionPane.INFORMATION_MESSAGE); t4.setText(""); t5.setText(""); t6.setText(""); } } catch(Exception e) { e.printStackTrace(); System.out.println("error in delete"); JOptionPane.showMessageDialog(null,"Invalid Account No","erro r occured",JOptionPane.ERROR_MESSAGE); } } public void closeDld(String acno) { try { t8.setText(""); t9.setText(""); t10.setText(""); rs=stmt.executeQuery("Select Name,Balance from a ccount where AccNo='"+acno+"'"); while(rs.next()) { t8.setText(rs.getString(1)); t9.setText(rs.getString(2));

} if(t8.getText().isEmpty()) { t7.setEditable(true); JOptionPane.showMessageDialog(null,"no match found","error occ ured",JOptionPane.ERROR_MESSAGE); } } catch(Exception e) { System.out.println("error in closeDld"); } } public void AccDeposit(String acno) { try { t8.setText(""); t9.setText(""); rs=stmt.executeQuery("Select Name,Balance from account where Acc No='"+acno+"'"); while(rs.next()) { t8.setText(rs.getString(1)); t9.setText(rs.getString(2)); } if(t8.getText().isEmpty()) { t7.setEditable(true); JOptionPane.showMessageDialog(null,"No Match Fou nd","error occured",JOptionPane.ERROR_MESSAGE); } else { int temp4=Integer.parseInt(t10.getText()); int dep=Integer.parseInt(t9.getText()); int deposit=dep+temp4; ps=con.prepareStatement("Update account Set Balance='"+deposi t+"' where AccNo='"+acno+"'"); ps.executeUpdate(); JOptionPane.showMessageDialog(null,"deposit successfully","Deposi t successfully",JOptionPane.INFORMATION_MESSAGE); t7.setText(""); t8.setText(""); t9.setText(""); t10.setText(""); t7.setEditable(true); } } catch(Exception e) { System.out.println("error in accDeposit"); } }

public void closeDlw(String accno) { try { t12.setText(""); t13.setText(""); rs=stmt.executeQuery("Select Name,Balance from a ccount where AccNo='"+accno+"'"); while(rs.next()) { t12.setText(rs.getString(1)); t13.setText(rs.getString(2)); } if(t12.getText().isEmpty()) { t11.setEditable(true); JOptionPane.showMessageDialog(null,"no match found","error occ ured",JOptionPane.ERROR_MESSAGE); } } catch(Exception e) { System.out.println("error in closeDlw"); } } public void AccWithdraw(String accno) { try { t12.setText(""); t13.setText(""); rs=stmt.executeQuery("Select Name,Balance from a ccount where AccNo='"+accno+"'"); while(rs.next()) { t12.setText(rs.getString(1)); t13.setText(rs.getString(2)); } if(t12.getText().isEmpty()) { t11.setEditable(true); JOptionPane.showMessageDialog(null,"no match found","error occ ured",JOptionPane.ERROR_MESSAGE); } else { int temp3=Integer.parseInt(t14.getText()); int Amo=Integer.parseInt(t13.getText()); if(Amo>temp3) { int Bal=Amo-temp3; ps=con.prepareStatement("Update account Set Balance='"+Bal+"' where AccNo='"+accno+"'"); ps.executeUpdate(); JOptionPane.showMessageDialog(null,"withdraw successfully","withd raw successfully",JOptionPane.INFORMATION_MESSAGE); t11.setEditable(true); t11.setText(""); t12.setText("");

t13.setText(""); t14.setText(""); } else { JOptionPane.showMessageDialog(null,"withdraw mo ney should be less then amount","error occured",JOptionPane.ERROR_MESSAGE); } } } catch(Exception e) { System.out.println("error in AccWithdraw in Dbinfo"); } } public void ShowAcc(boolean sign) { try { if(sign) { rs=stmt.executeQuery("Select AccNo from account"); rs.last(); int ann=Integer.parseInt(rs.getString(1)); Integer annn=new Integer(ann+1); String ss=annn.toString(); t1.setText(ss); sign=false; } else { rs=stmt.executeQuery("Select AccNo from account"); rs.last(); int ann=Integer.parseInt(rs.getString(1)); Integer annn=new Integer(ann+1); String ss=annn.toString(); t1.setText(ss); } } catch(Exception e) { e.printStackTrace(); System.out.println("error in Accno fetch"); } } } class Open extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if(flag1==false) { JFrame jf1=new JFrame(); setLayout(null); setSize(450,380); setLocation(100,20); setTitle("Open Account");

l1=new JLabel("Account no."); l1.setBounds(25, 75, 150, 30); l2=new JLabel("Name"); l2.setBounds(50, 115, 150, 30); l3=new JLabel("Amount"); l3.setBounds(50, 150, 150, 30); t1=new JTextField(15); t1.setBounds(100, 75, 150, 20); Dbinfo db1=new Dbinfo(); db1.ShowAcc(sign); t1.setEditable(false); t2=new JTextField(15); t2.setBounds(100, 115, 150, 20); t3=new JTextField(15); t3.setBounds(100, 155, 150, 20); b1=new JButton("Save"); b1.setBounds(55, 200, 100, 30); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x1) { try { String Name=t2.getText(); String check=t3.getText(); if(Name.isEmpty()) { JOptionPane.showMessageDialog(null,"Name can't be le ft blank","error occured",JOptionPane.ERROR_MESSAGE); } else { if(check.isEmpty()) { JOptionPane.showMessageDialog(null,"amou nt can't be left blank","error occured",JOptionPane.ERROR_MESSAGE); } else { if(ckname()) { JOptionPane.showMessageDialog(null,"only alphaba tes can be used in name","error occured",JOptionPane.ERROR_MESSAGE); } else { int Balance=Integer.parseInt(t3.getText()); if(Balance>=0) { Dbinfo db=new Dbinfo(); db.insert(Name,Balance); t2.setText("");t3.setText(""); } else {

JOptionPane.showMessageDialog(null,"Amount shoul d be more then zero","error occured",JOptionPane.ERROR_MESSAGE); t3.setText(""); } } } } } catch(Exception e) { JOptionPane.showMessageDialog(null,"Amount should be numeric","e rror occured",JOptionPane.ERROR_MESSAGE); t3.setText(""); } } }); b2=new JButton("Cancel"); b2.setBounds(190, 200, 100, 30); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x1) { t2.setText(""); t3.setText(""); } }); b3=new JButton("Close"); b3.setBounds(330, 200, 100, 30); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x1) { flag1=false; t2.setText(""); t3.setText(""); setVisible(false); } }); add(l1);add(t1); add(l2);add(t2); add(l3);add(t3); add(b1);add(b2); add(b3); addWindowListener(new mylistener()); repaint(); setResizable(false); setVisible(true); flag1=true; } else { JOptionPane.showMessageDialog(null,"This tab is already running","error occured",JOptionPane.ERROR_MESSAGE); } } class mylistener extends WindowAdapter { public void windowClosing (WindowEvent e)

{ flag1=false; t2.setText(""); t3.setText(""); setVisible(false); } } public boolean ckname() { String line=new String(t2.getText()); char arr[]=line.toCharArray(); Pattern p=Pattern.compile("\\d"); Matcher m=p.matcher(line); Pattern p1=Pattern.compile("\\w"); Matcher m1=p1.matcher(line); Pattern p2=Pattern.compile("\\s"); Matcher m2=p2.matcher(line); int www,c=0,k=0,sp=0; while(m.find()) { c++; } while(m1.find()) { k++; } while(m2.find()) { sp++; } www=k-c+sp; if(arr.length!=www) { return true; } else { return false; } } } class Close extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if(flag2==false) { JFrame jf2=new JFrame(); setLayout(null); setSize(450,380); setLocation(700,10); setTitle("Close Account"); l4=new JLabel("Account no."); l4.setBounds(25, 75, 150, 30);

l5=new JLabel("Name"); l5.setBounds(50, 115, 150, 30); l6=new JLabel("Amount"); l6.setBounds(50, 150, 150, 30); t4=new JTextField(15); t4.setBounds(100, 75, 150, 20); t4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x) { if(t4.getText().isEmpty()) { t5.setText("");t6.setText(""); } else { String acn=t4.getText(); Dbinfo db=new Dbinfo(); db.closeDl(acn); } } }); t5=new JTextField(15); t5.setBounds(100, 115, 150, 20); t5.setEditable(false); t6=new JTextField(15); t6.setBounds(100, 155, 150, 20); t6.setEditable(false); b4=new JButton("Delete"); b4.setBounds(55, 200, 100, 30); b4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x2) { try { String acn=t4.getText(); if(acn.isEmpty()) { JOptionPane.showMessageDialog(null,"fill value in Account No"," error occured",JOptionPane.ERROR_MESSAGE); } else { int temp5=Integer.parseInt(t4.getText()); Dbinfo db=new Dbinfo(); db.Delete(acn); } } catch(Exception e) { JOptionPane.showMessageDialog(null,"Invalid Account No","error o ccured",JOptionPane.ERROR_MESSAGE); } } }); b5=new JButton("Cancel");

b5.setBounds(190, 200, 100, 30); b5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x1) { t4.setText(""); t5.setText(""); t6.setText(""); } }); b6=new JButton("Close"); b6.setBounds(330, 200, 100, 30); b6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x1) { flag2=false; t4.setText(""); t5.setText(""); t6.setText(""); setVisible(false); } }); add(l4);add(t4); add(l5);add(t5); add(l6);add(t6); add(b4);add(b5); add(b6); addWindowListener(new mylistener1()); repaint(); setResizable(false); setVisible(true); flag2=true; } else { JOptionPane.showMessageDialog(null,"This tab is already running" ,"error occured",JOptionPane.ERROR_MESSAGE); } } class mylistener1 extends WindowAdapter { public void windowClosing (WindowEvent e) { flag2=false; t4.setText(""); t5.setText(""); t6.setText(""); setVisible(false); } } } class Deposit extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if(flag3==false)

{ JFrame jf3=new JFrame(); setLayout(null); setSize(450,380); setLocation(100,400); setTitle("Deposit Window"); l7=new JLabel("Account no."); l7.setBounds(25, 75, 150, 30); l8=new JLabel("Name"); l8.setBounds(50, 115, 150, 30); l9=new JLabel("Amount"); l9.setBounds(50, 150, 150, 30); l10=new JLabel("Deposit Amount"); l10.setBounds(15, 190, 150, 30); t7=new JTextField(15); t7.setBounds(120, 75, 150, 20); t7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x) { if(t7.getText().isEmpty()) { t8.setText("");t9.setText("");t10.setText(""); } else { String acno=t7.getText(); t7.setEditable(false); Dbinfo db=new Dbinfo(); db.closeDld(acno); } } }); t8=new JTextField(15); t8.setBounds(120, 115, 150, 20); t8.setEditable(false); t9=new JTextField(15); t9.setBounds(120, 155, 150, 20); t9.setEditable(false); t10=new JTextField(15); t10.setBounds(120, 195, 150, 20); b7=new JButton("Update"); b7.setBounds(55, 250, 100, 30); b7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x3) { try { String acno=t7.getText();

if(acno.isEmpty()) { JOptionPane.showMessageDialog(null,"you have not entered Acco unt no.","error occured",JOptionPane.ERROR_MESSAGE); } else { String dmo=t10.getText(); if(dmo.isEmpty()) { JOptionPane.showMessageDialog(null,"you have not en tered deposit money yet","error occured",JOptionPane.ERROR_MESSAGE); } else { if(checked()) { JOptionPane.showMessageDialog(null,"Deposit mon ey should be numeric","error occured",JOptionPane.ERROR_MESSAGE); } else { int temp=Integer.parseInt(t10.getText()); if(temp>0) { Dbinfo db=new Dbinfo(); db.AccDeposit(acno); } else { JOptionPane.showMessageDialog(null,"depo sit money should be more then zero *0*","error occured",JOptionPane.ERROR_MESSAG E); } } } } } catch(Exception e) { JOptionPane.showMessageDialog(null,"Account no. not exist,enter a valid and numeric Account no.","error occured",JOptionPane.ERROR_MESSAGE); } } public boolean checked() { try { int semi1=Integer.parseInt(t10.getText()); return false; } catch(Exception e) { return true; } } });

b8=new JButton("Cancel"); b8.setBounds(190, 250, 100, 30); b8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x1) { t7.setText(""); t8.setText(""); t9.setText(""); t10.setText(""); t7.setEditable(true); } }); b9=new JButton("Close"); b9.setBounds(330, 250, 100, 30); b9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x1) { flag3=false; t7.setText(""); t8.setText(""); t9.setText(""); t10.setText(""); t7.setEditable(true); setVisible(false); } }); add(l7);add(t7); add(l8);add(t8); add(l9);add(t9); add(l10);add(t10); add(b7);add(b8); add(b9); addWindowListener(new mylistener2()); repaint(); setResizable(false); setVisible(true); flag3=true; } else { JOptionPane.showMessageDialog(null,"This tab is already running","error occured",JOptionPane.ERROR_MESSAGE); } } class mylistener2 extends WindowAdapter { public void windowClosing (WindowEvent e) { flag3=false; t7.setText(""); t8.setText(""); t9.setText(""); t10.setText(""); t7.setEditable(true); setVisible(false); } }

} class Withdraw extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if(flag4==false) { JFrame jf4=new JFrame(); setLayout(null); setSize(450,380); setLocation(600,400); setTitle("Withdraw Window"); l11=new JLabel("Account no."); l11.setBounds(25, 75, 150, 30); l12=new JLabel("Name"); l12.setBounds(50, 115, 150, 30); l13=new JLabel("Amount"); l13.setBounds(50, 150, 150, 30); l14=new JLabel("Withdraw Amount"); l14.setBounds(15, 190, 150, 30); t11=new JTextField(15); t11.setBounds(120, 75, 150, 20); t11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x) { if(t11.getText().isEmpty()) { t12.setText("");t13.setText("");t14.setText(""); } else { String accno=t11.getText(); t11.setEditable(false); Dbinfo db=new Dbinfo(); db.closeDlw(accno); } } }); t12=new JTextField(15); t12.setBounds(120, 115, 150, 20); t12.setEditable(false); t13=new JTextField(15); t13.setBounds(120, 155, 150, 20); t13.setEditable(false); t14=new JTextField(15); t14.setBounds(120, 195, 150, 20); b10=new JButton("Update"); b10.setBounds(55, 250, 100, 30); b10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x4)

{ String accno=t11.getText(); try { int check3=Integer.parseInt(t11.getText()); if(accno.isEmpty()) { JOptionPane.showMessageDialog(null,"you have not entered Acco unt no.","error occured",JOptionPane.ERROR_MESSAGE); } else { String wmo=t14.getText(); if(wmo.isEmpty()) { JOptionPane.showMessageDialog(null,"you have not en tered withdraw money yet","error occured",JOptionPane.ERROR_MESSAGE); } else { if(checking()) { JOptionPane.showMessageDialog(null,"enter numeri c value in withdraw money","error occured",JOptionPane.ERROR_MESSAGE); } else { int temp2=Integer.parseInt(t14.getText()); if(temp2>0) { Dbinfo db=new Dbinfo(); db.AccWithdraw(accno); } else { JOptionPane.showMessageDialog(null,"with draw money should be more then zero *0*","error occured",JOptionPane.ERROR_MESSA GE); } } } } } catch(Exception e) { JOptionPane.showMessageDialog(null,"Account no. not exist,enter a valid and numeric Account no.","error occured",JOptionPane.ERROR_MESSAGE); } } public boolean checking() { try

{ int semi2=Integer.parseInt(t14.getText()); return false; } catch(Exception e) { return true; } } }); b11=new JButton("Cancel"); b11.setBounds(190, 250, 100, 30); b11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x) { t11.setText(""); t12.setText(""); t13.setText(""); t14.setText(""); t11.setEditable(true); } }); b12=new JButton("Close"); b12.setBounds(330, 250, 100, 30); b11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent x) { flag4=false; t11.setText(""); t12.setText(""); t13.setText(""); t14.setText(""); t11.setEditable(true); setVisible(false); } }); add(l11);add(t11); add(l12);add(t12); add(l13);add(t13); add(l14);add(t14); add(b10);add(b11); add(b12); addWindowListener(new mylistener3()); repaint(); setResizable(false); setVisible(true); flag4=true; } else { JOptionPane.showMessageDialog(null,"This tab is already running","error occured",JOptionPane.ERROR_MESSAGE); } } class mylistener3 extends WindowAdapter { public void windowClosing (WindowEvent e) {

flag4=false; t11.setText(""); t12.setText(""); t13.setText(""); t14.setText(""); t11.setEditable(true); setVisible(false); } } } class Exit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } class mylistener4 extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } public static void main(String arg[]) { BManage bmg=new BManage(); } }

You might also like