Beruflich Dokumente
Kultur Dokumente
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import java.awt.event.KeyEvent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.FlowLayout;
import java.sql.*;
JTextField
tfAccNo,tfName,tfAmmount,close_tfAccNo,close_tfName,close_tfAmt,deposit_tfAccNo
,deposit_tfName,deposit_tfBal,deposit_tfAmt;
BankSimulator()
{
frame = new JFrame();
menubar = new JMenuBar();
frame.add(menubar);
mAccount.add(miNew);
mAccount.add(miClose);
mAccount.add(miExit);
mTransaction.add(miDeposit);
mTransaction.add(miWithdraw);
menubar.add(mAccount);
menubar.add(mTransaction);
frame.setJMenuBar(menubar);
frame.add(desktopPane);
frame.setVisible(true);
frame.setBounds(0,0,800,600);
miNew.addActionListener(this);
miClose.addActionListener(this);
miExit.addActionListener(this);
miDeposit.addActionListener(this);
miWithdraw.addActionListener(this);
//--------------------------------------------------------------------------
}
public void actionPerformed(ActionEvent ae)
{
JMenuItem jmi = null;
jmi = (JMenuItem)ae.getSource();
if(jmi == miNew)
{
newAccount = new JInternalFrame("New
Account",false,true,false,true); // create JINTERNAL FRAME
newAccount.setBounds(200,200,320,200);
// newAccount.setBounds(30,30,dimension.width - 300,
dimension.height - 300);
newAccount.setLayout(new FlowLayout());
newAccount.add(lbAccNo);
newAccount.add(tfAccNo);
newAccount.add(lbName);
newAccount.add(tfName);
newAccount.add(lbAmount);
newAccount.add(tfAmmount);
newAccount.add(create);
newAccount.add(cancel);
newAccount.add(close);
}
catch(Exception ex)
{
System.out.println
("-------------------------------"+ex);
ex.printStackTrace();
}
finally
{
try{ConnectionClass.closed();}
catch(Exception es){es.printStackTrace();}
}
//--------------------------------------------------------------------
}
if(jmi == miClose)
{
closeAccount = new JInternalFrame("Close
Accout",false,true,false,true);
closeAccount.setBounds(200,200,320,200);
closeAccount.setLayout(new FlowLayout());
closeAccount.add(lbAccNo);
closeAccount.add(close_tfAccNo);
closeAccount.add(lbName);
closeAccount.add(close_tfName);
closeAccount.add(lbAmt);
closeAccount.add(close_tfAmt);
closeAccount.add(close_button_yes);
closeAccount.add(close_button_no);
closeAccount.add(close_button_close);
if(jmi == miDeposit)
{
depositAccount = new
JInternalFrame("Deposit",false,true,false,true);
depositAccount.setBounds(200,200,320,200);
depositAccount.setLayout(new FlowLayout());
depositAccount.add(lbAccNo);
depositAccount.add(deposit_tfAccNo);
depositAccount.add(lbName);
depositAccount.add(deposit_tfName);
depositAccount.add(lbBal);
depositAccount.add(deposit_tfBal);
depositAccount.add(lbAmt);
depositAccount.add(deposit_tfAmt);
depositAccount.add(deposit_button_update);
depositAccount.add(deposit_button_cancel);
depositAccount.add(deposit_button_close);
// ----------------------- Adding Listner to Internal Frame
components------------
deposit_tfAccNo.addActionListener(new
InternalActionListener());
deposit_button_update.addActionListener(new
InternalActionListener());
deposit_button_cancel.addActionListener(new
InternalActionListener());
deposit_button_close.addActionListener(new
InternalActionListener());
//- --------------------------------------------------------------------------------
desktopPane.add(depositAccount);
depositAccount.setVisible(true);
if(jmi == miWithdraw)
{
withdrawAccount = new
JInternalFrame("Withdraw",false,true,false,true);
withdrawAccount.setBounds(200,200,320,200);
withdrawAccount.setLayout(new FlowLayout());
withdrawAccount.add(lbAccNo);
withdrawAccount.add(withdraw_tfAccNo);
withdrawAccount.add(lbName);
withdrawAccount.add(withdraw_tfName);
withdrawAccount.add(lbBal);
withdrawAccount.add(withdraw_tfBal);
withdrawAccount.add(lbAmt);
withdrawAccount.add(withdraw_tfAmt);
withdrawAccount.add(withdraw_button_update);
withdrawAccount.add(withdraw_button_cancel);
withdrawAccount.add(withdraw_button_close);
desktopPane.add(withdrawAccount);
withdrawAccount.setVisible(true);
}
}
// Internal class for maintain internal action listener------------------------
//------------------------------------------------------------------------
public class InternalActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(button!=null)
{
if(button.getText().equals("Create"))
{
try
{
Connection con =
ConnectionClass.getConnection();
Statement stmt = con.createStatement();
String query = "insert into
bank.banktable(Name,accountNo,amount) values('"+name+"','"+
account+"','"+ammount+"')";
stmt.execute(query);
}
catch(Exception ex)
{
System.out.println ("hello"+ex);
}
finally
{
try{ConnectionClass.closed();}
catch(Exception ex){}
}
if(button.getText().equals("Cancel"))
{
System.out.println("I m in Cancle");
tfAmmount.setText("");
tfName.setText("");
}
if(button.getText().equals("Close") ||
button.getText().equals("CLOSE"))
{
System.out.println("I m in Close");
if(button.getText().equals("Close"))
newAccount.dispose();
if(button.getText().equals("CLOSE"))
closeAccount.dispose();
}
if(button.getText().equals("YES"))
{
try
{
Connection con = ConnectionClass.getConnection();
System.out.println ("Conn create ");
Statement stmt = con.createStatement();
String accno = close_tfAccNo.getText();
System.out.println (accno);
stmt.execute("delete from bank.banktable where
accountNo = "+accno+"");
}
catch(Exception ex)
{
System.out.println
("-------------------------------"+ex);
ex.printStackTrace();
}
finally
{
try{ConnectionClass.closed();}
catch(Exception es){es.printStackTrace();}
}
}
if(button.getText().equals("NO"))
{
JOptionPane.showMessageDialog(null,"dfdf");
closeAccount.dispose();
}
if(textField!=null)
{
System.out.println("");
close_tfName.setEditable(false);
System.out.println(textField);
try
{
Connection con = ConnectionClass.getConnection();
System.out.println ("Conn create ");
Statement stmt = con.createStatement();
String accno = close_tfAccNo.getText();
System.out.println (accno);
ResultSet rs = stmt.executeQuery("SELECT
Name,amount FROM banktable where accountNo='"+accno+"'");
close_tfName.setText("");
close_tfAmt.setText("");
if(rs.next())
{
String name = rs.getString("Name");
System.out.println (name);
close_tfName.setText(name);
System.out.println (rs.getInt("amount"));
int amount = rs.getInt("amount");
close_tfAmt.setText(Integer.toString(amount));
ConnectionClass.closed();
}
catch(Exception ex)
{
System.out.println
("-------------------------------"+ex);
ex.printStackTrace();
}
finally
{
try{ConnectionClass.closed();}
catch(Exception es){es.printStackTrace();}
}
}
}
class ConnectionClass
{
static Connection con =null;
static
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e)
{
e.printStackTrace();
}
}
String url="jdbc:mysql://localhost:3306/bank";
con=DriverManager.getConnection(url,"root","root");
System.out.println("connection estbalish");
return con;
}
catch(Exception e)
{
System.out.println ("I am in --
ConnectionClass/getConnection()/catch ");
e.printStackTrace();
return con;
}
}