2012-03-17 185 views
0

我一直在努力讓客戶端和服務器發送和接收正確的信息。程序應該能夠從客戶端向服務器發送查詢。然後服務器應該從數據庫中檢索必要的信息並以數組的形式發送給客戶端。從MySql數據庫從Java服務器獲取信息到客戶端

Java服務器:

public class Server { 
    public static void main(String[] args) throws Exception { 
     ServerSocket m_ServerSocket = new ServerSocket(1211, 2, InetAddress.getLocalHost()); 
     int id = 0; 
     while (true) { 
      Socket clientSocket = m_ServerSocket.accept(); 
      System.out.println(clientSocket.isConnected()); 


      ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++); 
      cliThread.start(); 
     } 
    } 
    } 

    class ClientServiceThread extends Thread 
    { 
    ResultSet res = null; 
    Connection conn; 


    ObjectOutputStream objOut; 
    PrintWriter out; 
    BufferedReader in; 

    Socket clientSocket; 
    int clientID = -1; 
    boolean running = true; 

    ClientServiceThread(Socket s, int i) 
    { 
     clientSocket = s; 
     clientID = i; 
    } 

    public void run() 
    { 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
      conn = DriverManager.getConnection("jdbc:mysql://localhost/videostore", "root", "IronFire"); 

     } 
     catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     try 
     { 
      System.out.println("Server"); 
      in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
      out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); 
      objOut= new ObjectOutputStream(clientSocket.getOutputStream()); 

      while (running) 
      { 
       String command = in.readLine(); 
       System.out.println("Query: " + command); 
       if(command.substring(0, command.indexOf(" ")).equalsIgnoreCase("SELECT+")) 
       { 
        System.out.println("IN IF"); // for test 
        surname(command); 
       } 
       if (command.equalsIgnoreCase("quit")) 
       { 
        running = false; 
        System.out.print("Stopping client thread for client : " + clientID); 
       } 
       else 
       { 
        out.println(command); 
        out.flush(); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    public void surname(String query) 
    { 
     try 
     { 
      System.out.println("In Surname"); // for test 
      int count = 0; 
      int whileCounter = 0; 
      Statement state = conn.createStatement(); 

      res = state.executeQuery(query.replace("+", "")); 

      while(res.next()) 
      { 
       count++; 
      } 
      String [] stringResult = new String[count]; 
      byte[] buff = new byte[count]; 
      res.beforeFirst(); 

      while(res.next()) 
      { 
       stringResult[whileCounter] = res.getString(1) +" "+ res.getString(2) +" " + res.getString(3) +" "+ res.getString(5) +" "+ res.getString(10) +" "+ res.getString(11) +""; 
       whileCounter++; 
      } 

      for(int i = 0; i < stringResult.length; i++) 
      { 
       System.out.println(stringResult[i]); 
      } 

      objOut.reset(); 
      objOut.writeObject(stringResult); 
      objOut.flush(); 


      System.out.println("After object"); 
      //objOut.close(); 


     } 
     catch(Exception ex) 
     { 

     } 

    } 
} 

Java客戶端:

public class FuncListener2 extends MainGui implements Runnable 
    { 
    ResultSet res = null; 
    Connection conn = null; 

    Socket clientSocket = null; 
    PrintStream os = null; 
    ObjectInputStream objIn = null; 
    ObjectOutputStream objOut = null; 


    public FuncListener2() 
    { 
     String hostName = ""; 

     try { 
      hostName = InetAddress.getLocalHost().toString(); // to get the hostName for the exceptions 
      clientSocket = new Socket(InetAddress.getLocalHost(), 1211); 
      System.out.println(clientSocket.isConnected()); 

      os = new PrintStream(clientSocket.getOutputStream()); 
      objIn = new ObjectInputStream(clientSocket.getInputStream()); 
      listeners(); 

      clientSocket.setKeepAlive(true); 
     } 
     catch (UnknownHostException e) 
     { 
      System.err.println("Don't know about host "+hostName); 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      System.err.println("Couldn't get I/O for the connection to the host "+hostName); 
      e.printStackTrace(); 
     } 
     catch(Exception ex) 
     { 
      System.err.println(ex.getMessage()); 
     } 
    } 

    public void listeners() 
    { 
     System.out.println("in listener"); 

     butCheckOutSearchSurname.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       try 
       { 
        System.out.println("surname search"); 
        int count = 0; 
        Object obj = null; 
        Object [] objArray = null; 
        String [] array = null; 

        // the reason for the plus sign is to make sure the correct if statement is executed in the server 
        String query = "Select+ * FROM clientDet WHERE clientSurname = \'" + textCheckOutSearchSurname.getText() + "\'"; 
        os.println(query); 

        if(objIn.readObject() != null) 
        { 
         objArray = (Object[]) objIn.readObject(); 
        } 
        else 
        { 
         System.out.println("object empty"); 
        } 
        objIn.reset(); 

        listCheckOutClients.setListData(array); 

        System.out.println("iets"); 
       } 
       catch(Exception ex) 
       { 
        ex.printStackTrace(); 
       } 
      } 
     }); 
    } 
    public void run() 
    {  

    } 
    } 

這個例外是到目前爲止我最大的問題。

java.io.StreamCorruptedException: invalid type code: 53 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) 
    at FuncListener2$1.actionPerformed(FuncListener2.java:108) 
+0

我很少見到流'reset'。 – blackcompe 2012-03-17 10:11:57

回答

1

您是不是打算撥打readObject兩次?你正在檢查它是否爲空,但是你不應該保存它,所以你沒有執行第二次讀取?

+0

感謝dldnh你現在正確的工作。 – IronFire 2012-03-17 11:58:39

相關問題