Breaking News
Loading...
Monday, 15 February 2016

sim project (s5)

08:15:00
siminfo.java

 -------------------button insert-----------
  private void btn_inActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:

        try{
          String cn =txt_cn.getText();
          String sn =txt_sn.getText();
          String cno =txt_cno.getText();
          String cb =txt_cb.getText();
          String ca =txt_ca.getText();

          message = "1:"+cn+":"+sn+":"+cno+":"+cb+":"+ca;

          output_stream.writeObject(message);
          output_stream.flush();
          System.out.println("Sent Message = "+message);

          String m=(String) input_stream.readObject();
          System.out.println("Message Recieved = "+m);

          }
        catch( IOException ioException ) {
        // taconversation.append( "\nError writing object" );
          JOptionPane.showMessageDialog(null,"Error writing Object");
          //ioException.printStackTrace();
      }
        catch ( ClassNotFoundException classNotFoundException ) {
          System.out.println( "\nUnknown object type received" );
      }
        catch ( Exception  ex ) {
          System.out.println( "\nUnknown object type received" );
      }
    }
   --------------------button search------------------
private void btn_serActionPerformed(java.awt.event.ActionEvent evt) {                                       
       try{
          String cn =txt_cn.getText();
          String sn =txt_sn.getText();
          String cno =txt_cno.getText();
          String cb =txt_cb.getText();
          String ca =txt_ca.getText();

          message = "3:"+sn;

          output_stream.writeObject(message);
          output_stream.flush();
          System.out.println(" Message = "+message);

          String m=(String) input_stream.readObject();
          System.out.println("Message Recieved = "+m);

          }
        catch( IOException ioException ) {
        // taconversation.append( "\nError writing object" );
          JOptionPane.showMessageDialog(null,"Error writing Object");
          //ioException.printStackTrace();
      }
        catch ( ClassNotFoundException classNotFoundException ) {
          System.out.println( "\nUnknown object type received" );
      }  // TODO add your handling code here:
    }                                      
------------------button delete --------------
private void btn_delActionPerformed(java.awt.event.ActionEvent evt) {                                       
        try{
          String cn =txt_cn.getText();
          String sn =txt_sn.getText();
          String cno =txt_cno.getText();
          String cb =txt_cb.getText();
          String ca =txt_ca.getText();

          message = "2:"+sn;

          output_stream.writeObject(message);
          output_stream.flush();
          System.out.println("Sent Message = "+message);

          String m=(String) input_stream.readObject();
          System.out.println("Message Recieved = "+m);

          }
        catch( IOException ioException ) {
        // taconversation.append( "\nError writing object" );
          JOptionPane.showMessageDialog(null,"Error writing Object");
          //ioException.printStackTrace();
      }
        catch ( ClassNotFoundException classNotFoundException ) {
          System.out.println( "\nUnknown object type received" );
      }
   
---------------button start connection------------------
private void btn_startActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try {
         client_sock = new Socket(
         InetAddress.getLocalHost(), 1133 );

         output_stream = new ObjectOutputStream(
         client_sock.getOutputStream() );
         output_stream.flush();
           input_stream = new ObjectInputStream(
         client_sock.getInputStream() );

         message = ( String ) input_stream.readObject();
         JOptionPane.showMessageDialog(null,message);
         }
         catch ( ClassNotFoundException classNotFoundException ) {
             System.out.println( "\nUnknown object type received" );
         }
         catch ( IOException ioException ) {
              JOptionPane.showMessageDialog(null, "chal koi baat nahi Check IO");
         }
    }                                        
-----------------------button close connection---------------
private void btn_closeActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try {
        closeConnection();

        }
        catch ( IOException ioException ) {
          JOptionPane.showMessageDialog(null,"we can't close");
        }
}                           

serverconection.java      (runtime)

 import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.text.html.*;
import java.sql.*;
import java.util.*;
import java.*;
public class ServerConn extends JFrame implements ActionListener{
        JTextField tfsendmessage;
        String sql;
        JTextArea taconversation;
        JButton btnsend,btnexit;
        JPanel pbuttons,ppanels,plabel;
        String s;
        ServerSocket server_sock;
        Socket sock_conn;
        ObjectOutputStream output_stream;
        ObjectInputStream input_stream;
        int count=1;
        JPanel p;
        String message;
        boolean connected = false;
               
        ServerConn()
        {
            super("serverConn");
            Container container =getContentPane();
            tfsendmessage = new JTextField();

            taconversation=new JTextArea();
            btnsend=new JButton("send");
            btnexit=new JButton("exit");
            pbuttons = new JPanel();
            ppanels=new JPanel();
            ppanels.add(tfsendmessage);
            pbuttons.add(btnsend);
            btnsend.addActionListener(this);
            pbuttons.add(btnexit);
            ppanels.add(pbuttons);
            p = new JPanel();

            ppanels.setLayout(new GridLayout(2,1));
            container.add(new JScrollPane(taconversation),BorderLayout.CENTER);
            container.add(ppanels,BorderLayout.SOUTH);

            setSize(350,500);
            setVisible(true);
        }
        public void serverfunc()
        {
            try{
               while(true)
               {
                server_sock=new ServerSocket(1133,10);
                connectionwait();
                ++count;
               }
            }
            catch(EOFException e){
              System.out.println("client disconnected");
            }
            catch(Exception ex){
              System.out.println("unknown exception caught");
            }
    }

    public void actionPerformed(ActionEvent e)
    {    if(e.getSource()==btnsend)
        {
            String msg = tfsendmessage.getText();
                   try{
                    output_stream.writeObject("SERVER>>>" + msg);
                    output_stream.flush();
                    taconversation.append("\n SERVER>>>" + msg);
                }
                catch(IOException x){
                    taconversation.append("\n error writing object");
                }
        }
    }
    void connectionwait() throws IOException
    {
        taconversation.setText("Server Waiting for Client's Request...\n");
        sock_conn =server_sock.accept();
        taconversation.append(sock_conn.getInetAddress().getHostName()+"connected");

        //if(!connected)
        //{
        output_stream=new ObjectOutputStream(sock_conn.getOutputStream());
        output_stream.flush();
        input_stream=new ObjectInputStream(sock_conn.getInputStream());
        taconversation.append("\n got io streams");

        message=" SERVER>>> connection successful";
        output_stream.writeObject(message);
        output_stream.flush();
        tfsendmessage.setEnabled(true);
        //connected = true;
        //}
        do
        {
            try
              {
              message=(String) input_stream.readObject();
              System.out.println(""+message);
              String[] t = message.split(":");
              int i= Integer.parseInt(t[0]);
                switch(i)
                {
                case 1:
                    {
                     sql="insert into Sim values('"+t[1]+"','"+t[2]+"','"+t[3]+"','"+t[4]+"','"+t[5]+"')";
                  try{
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=E:\\Advanced OOP\\Sim\\Sim_Info.mdb");
                    Statement stmt   = conn.createStatement();
                    stmt.execute(sql);
                    String m="Record has Entered";
                    output_stream.writeObject(m);
                    //conn.close();
                    }
                   catch(java.lang.ClassNotFoundException ex){
                       JOptionPane.showMessageDialog(null,ex.getMessage());
                    }
                   catch(Exception ex){
                       JOptionPane.showMessageDialog(null,ex.getMessage());
                        }
                 break;
                    }
                case 2:
                    {
                     sql="delete from Sim where Sim_S_No=('"+t[1]+"')";
                  try{
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:\\Sim\\Sim_Info.mdb");
                    Statement stmt   = conn.createStatement();
                    stmt.execute(sql);
                    String m="Record has deleted";
                    output_stream.writeObject(m);
                    //conn.close();
                    }
                   catch(java.lang.ClassNotFoundException ex){
                       JOptionPane.showMessageDialog(null,ex.getMessage());
                    }
                   catch(Exception ex){
                       JOptionPane.showMessageDialog(null,ex.getMessage());
                        }
                 break;
                    }
                case 3:
                     {
                     sql="select* from Sim where Sim_S_No=('"+t[1]+"')";
                  try{
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:\\Sim\\Sim_Info.mdb");
                    Statement stmt   = conn.createStatement();
                    stmt.execute(sql);
                    String m="Record has searched";
                    output_stream.writeObject(m);
                    //conn.close();
                    }
                   catch(java.lang.ClassNotFoundException ex){
                       JOptionPane.showMessageDialog(null,ex.getMessage());
                    }
                   catch(Exception ex){
                       JOptionPane.showMessageDialog(null,ex.getMessage());
                        }
                 break;
                    }
                }
             }
          catch(ClassNotFoundException classnotfoundexception){
             taconversation.append("\n unknown object type received");
             }
            connectionwait();
        }
        while(!message.equals("End"));
        closeconnection();
    }

void closeconnection() throws IOException
            {
                taconversation.append("\n user terminated connection");
                tfsendmessage.setEnabled(false);
                output_stream.close();
                input_stream.close();
                sock_conn.close();
            }

public static void main(String [] args)
        {
        ServerConn first=new ServerConn();
        first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        first.serverfunc();
    }
}

0 comments:

Post a Comment

 
Toggle Footer