Monday, April 16, 2007

BankServer

import java.sql.*;
import java.sql.Date.*;
import javax.swing.*;
import java.awt.*;
import java.awt.print.*;
import java.awt.event.*;
import java.io.*;
import java.util.Calendar.*;
import java.util.Date.*;
import java.text.*;
import java.net.*;

class BankServer extends JFrame {
private JTabbedPane tabbedPane;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JButton addbutton;
private JTextField field,field2,fie1,fie2,fie3,fiee1,fiee2,fiee3;
private String amm="";
ObjectOutputStream output;
ObjectInputStream input;
private JTextArea display;
private int amountt;

public BankServer()
{
setTitle( "Banking Program" );
setSize( 400, 300 );
setBackground( Color.gray );

JPanel topPanel = new JPanel();
display = new JTextArea();
field = new JTextField();
field2 = new JTextField();
fie1 = new JTextField();
fie2 = new JTextField();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
createPage1();
createPage2();
createPage3();

// Create a tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Add Client", panel1 );
tabbedPane.addTab( "With Draw", panel2 );
tabbedPane.addTab( "Deposit", panel3 );
topPanel.add( tabbedPane, BorderLayout.CENTER );

}

public void runServer(){
ServerSocket server;
Socket connection;
int counter = 1;
try{ server = new ServerSocket(5000,100);
while(true){
display.setText("Waiting for connection\n");
connection = server.accept();
display.append("Connection"+ counter + " recieved from: " + connection.getInetAddress().getHostName());
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
display.append("\n Got I/O stream\n");
String message ="";// "SERVER>> Connection Succesfull";
do{
try {

String getrequest=(String) input.readObject();
if(getrequest.equals("add")){
String oktoadd= "oktoadd";
output.writeObject(oktoadd);
//field.setText((String) input.readObject());
//field2.setText((String)input.readObject());

field.setText((String) input.readObject());
field2.setText((String)input.readObject());
String okk=(String)input.readObject();
if(okk.equals("ok")){
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Bank";
Connection con = DriverManager.getConnection(url,"awing","awing");
Statement st = con.createStatement();

st.executeUpdate("INSERT INTO ClientTable( Client_ID , Name) "+
" VALUES ( '" + field.getText()+"','" + field2.getText() + "') ");
con.close();
JOptionPane.showMessageDialog(null,"Enroll is Succesfull");
field.setText("");
field2.setText("");
} catch (java.lang.Exception ex){
//ex.printStackTrace();
JOptionPane.showMessageDialog(null,"Add is not Succesfull");
field.setText("");
field2.setText("");
String eror="error";
//output.writeObject(eror);
senderror(eror);
}
}//end if
} //end if

if(getrequest.equals("withdraw")){
String oktowithdraw= "oktowithdraw";
output.writeObject(oktowithdraw);
fie1.setText((String) input.readObject());
String mewith = fie1.getText(),oo="ok";
if(oo.equals("ok")){
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Bank";
Connection con = DriverManager.getConnection(url,"awing","awing");
Statement st = con.createStatement();
String query1= "SELECT * FROM ClientTable WHERE Client_ID='"+fie1.getText()+"' ";
ResultSet rs1 = st.executeQuery(query1);
rs1.next();

fie2.setText(rs1.getString(2));
String name = fie2.getText();
//output.writeObject(name);
sendData(name);
} catch (java.lang.Exception ex)
{
//ex.printStackTrace();
JOptionPane.showMessageDialog(null,"invalid Client ID or None in database");
}
}//end if sa withdraw....

//fie2.setText(
//String okk=(String)input.readObject();


}
}
catch(ClassNotFoundException cnfex){
display.append("\nUnknown object type received");
}
}while(!message.equals("CLIENT>>> TERMINATE"));
display.append("\n User terminated connection");
//enter.setEnabled(false);
output.close();
input.close();
connection.close();
++counter;
}
}
catch(IOException io){
io.printStackTrace();
}
}

public void createPage1() {
panel1 = new JPanel();
panel1.setLayout( null );

JLabel label1 = new JLabel( "Client ID:" );
label1.setBounds( 10, 15, 150, 20 );
panel1.add( label1 );

//field = new JTextField();
field.setBounds( 60, 15, 80, 20 );
panel1.add( field );

JLabel label2 = new JLabel( "Name:" );
label2.setBounds( 10, 45, 150, 20 );
panel1.add( label2 );

//field2 = new JTextField();
field2.setBounds( 60, 45, 150, 20 );
panel1.add( field2 );
addbutton = new JButton("Add");
display.setBounds(50, 150, 230, 75 );
display.setEditable(false);
panel1.add(display);
addbutton.setBounds(100, 100, 90, 20 );
addbutton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Bank";
Connection con = DriverManager.getConnection(url,"awing","awing");
Statement st = con.createStatement();

st.executeUpdate("INSERT INTO ClientTable( Client_ID , Name) "+
" VALUES ( '" + field.getText()+"','" + field2.getText() + "') ");
con.close();
//JOptionPane.showMessageDialog(null,"Enroll is Succesfull");
field.setText("");
field2.setText("");
} catch (java.lang.Exception ex){
//ex.printStackTrace();
JOptionPane.showMessageDialog(null,"Add is not Succesfull");

field.setText("");
field2.setText("");
}
}
});
panel1.add( addbutton);
}

public void createPage2() {
panel2 = new JPanel();
panel2.setLayout( null);
panel2.setBackground( Color.blue );
JLabel lab1 = new JLabel( "Client ID:" );
lab1.setBounds( 10, 15, 150, 20 );
panel2.add( lab1 );

//fie1 = new JTextField();
fie1.setBounds( 60, 15, 80, 20 );
panel2.add( fie1 );

JLabel lab2 = new JLabel( "Name:" );
lab2.setBounds( 10, 45, 150, 20 );
panel2.add( lab2 );

//fie2 = new JTextField();
fie2.setBounds( 60, 45, 150, 20 );
fie2.setEditable(false);
panel2.add( fie2 );
JLabel amount = new JLabel("Amount:");
amount.setBounds( 10, 70, 150, 20 );
panel2.add(amount);
fie3 = new JTextField();
fie3.setBounds( 60, 70, 150, 20 );

panel2.add(fie3);
fie1.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Bank";
Connection con = DriverManager.getConnection(url,"awing","awing");
Statement st = con.createStatement();
String query1= "SELECT * FROM ClientTable WHERE Client_ID='"+fie1.getText()+"' ";
ResultSet rs1 = st.executeQuery(query1);
rs1.next();

fie2.setText(rs1.getString(2));

} catch (java.lang.Exception ex)
{
//ex.printStackTrace();
JOptionPane.showMessageDialog(null,"invalid Client ID or None in database");
}
}
});
JButton okbutton = new JButton("with draw");
okbutton.setBounds(100, 100, 90, 20 );

okbutton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
amm = fie3.getText();
amountt = Integer.parseInt(amm);
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Bank";
Connection con = DriverManager.getConnection(url,"awing","awing");
Statement st = con.createStatement();
st.executeUpdate("INSERT INTO ClientWithdraw( Client_ID , Amount) "+
" VALUES ( '" + fie1.getText()+"','" + amountt + "') ");
con.close();
JOptionPane.showMessageDialog(null,"WithDraw is Succesfull");

} catch (java.lang.Exception ex)
{
//ex.printStackTrace();
JOptionPane.showMessageDialog(null,"Add is not Succesfull");
}
}
});
panel2.add( okbutton);
}

private void senderror( String s ) {
try {
//message = s;
output.writeObject ( s );
output.flush();
//display.append ( "\nCLIENT>>> " + s );
}
catch ( IOException cnfe ) {
display.append ( "\nError writing object" );
}
}
private void sendData( String s ) {
try {
//message = s;
output.writeObject ( s );
output.flush();
//display.append ( "\nCLIENT>>> " + s );
}
catch ( IOException cnfe ) {
display.append ( "\nError writing object" );
}
}

public void createPage3()
{
panel3 = new JPanel();
panel3.setLayout( null );
panel3.setBackground( Color.orange );

JLabel labb1 = new JLabel( "Client ID:" );
labb1.setBounds( 10, 15, 150, 20 );
panel3.add( labb1 );

JTextField fiee1 = new JTextField();
fiee1.setBounds( 60, 15, 80, 20 );
panel3.add( fiee1 );

JLabel labb2 = new JLabel( "Name:" );
labb2.setBounds( 10, 45, 150, 20 );
panel3.add( labb2 );

JTextField fiee2 = new JTextField();
fiee2.setBounds( 60, 45, 150, 20 );
fiee2.setEditable(false);
panel3.add( fiee2 );
JLabel amoun = new JLabel("Amount:");
amoun.setBounds( 10, 70, 150, 20 );
panel3.add(amoun);
fiee3 = new JTextField();
fiee3.setBounds( 60, 70, 150, 20 );
panel3.add(fiee3);
JButton okkbutton = new JButton("deposit");
okkbutton.setBounds(100, 100, 90, 20 );

panel3.add( okkbutton);
}

public static void main( String args[] )
{
// Create an instance of the test application
BankServer mainFrame = new BankServer();
mainFrame.setVisible( true );
mainFrame.runServer();
}
}

1 comment:

ismail said...

thank you sir

but can you post the bankclient..??




.