如何从TCP服务器将JavaFX表填充到TCP客户端,从TCP检索数据

时间:2018-06-03 09:34:18

标签: java database javafx tcp

我的任务是通过数据库中的服务器从客户端发送数据,并通过服务器检索这些数据并填写一些表格。

我找不到如何以我想要的方式填写这些表的方法。

GitHub项目link

我从中获取客户端数据的客户端侦听器

public clientListener(TextArea textArea) {
        this.textArea = textArea;
        int portNr = Integer.parseInt(port);

        try {
            // Create a socket to connect to the server
            @SuppressWarnings("resource")
            Socket socket = new Socket("localhost", portNr);

            // Create an output stream to send data to the server
            outputToServer = new PrintWriter(socket.getOutputStream());

            // Create an input stream to read data from the server
            inputFromServer = new BufferedReader(new 
            InputStreamReader(socket.getInputStream()));

        } catch (IOException ex) {
            Platform.runLater(() -> textArea.appendText("Exception in gateway constructor: " + ex.toString() + "\n"));
        }
    } 

    public ObservableList<Qyteti> getQytet() {
        outputToServer.println(GET_QYTET);
        outputToServer.flush();
        String ID = null, Qyteti = null, ZIP = null;
        try {
            ObservableList<Qyteti> Qytet = null;
            Qytet.add(new Qyteti(inputFromServer.readLine().concat(ID), 
            inputFromServer.readLine().concat(Qyteti), 
            inputFromServer.readLine().concat(ZIP)));
            return Qytet;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

表单控制器

public void updateTable() {
    ID.setCellValueFactory(new PropertyValueFactory<>("ID"));
    Qyteti.setCellValueFactory(new PropertyValueFactory<>("Qyteti"));
    ZIP.setCellValueFactory(new PropertyValueFactory<>("ZIP"));
    QytetiList.add((application.Qyteti) gateway.getQytet());    
    tblQyteti.setItems(QytetiList);
}

服务器功能从不同情况下我获得不同的结果

public void run() {
    handler = new ConnectionHandler();
    try {
    // Create reading and writing streams
    BufferedReader inputFromClient = new BufferedReader(new 
    InputStreamReader(socket.getInputStream()));
    PrintWriter outputToClient = new PrintWriter(socket.getOutputStream());

    // Continuously serve the client
    while (true) {
      // Receive request code from the client
      int request = Integer.parseInt(inputFromClient.readLine());
      // Process request
      switch(request) {
          case GET_QYTET: {
              connection = handler.getConnection();
              String merrQytet = "SELECT * FROM tblQyteti";
              try {
                  pst = connection.prepareStatement(merrQytet);
                  ResultSet rs = pst.executeQuery();
                  while(rs.next()) { 
                    outputToClient.printf(merrQytet, rs.getString("qid"), rs.getString("Qyteti"), rs.getString("ZIP"));
                  }
              } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }
      }
    }
  }
  catch(IOException ex) {
        Platform.runLater(()->textArea.appendText("Exception in client 
        thread: "+ex.toString()+"\n"));
  }

用作对象类型的Qyteti类:

import java.io.Serializable;

public class Qyteti implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    String ID, Qyteti, ZIP;

    public Qyteti(String ID, String Qyteti, String ZIP) {
        this.ID = ID;
        this.Qyteti = Qyteti;
        this.ZIP = ZIP;
    }

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }

    public String getQyteti() {
        return Qyteti;
    }

    public void setQyteti(String qyteti) {
        Qyteti = qyteti;
    }

    public String getZIP() {
        return ZIP;
    }

    public void setZIP(String zIP) {
        ZIP = zIP;
    }


}

0 个答案:

没有答案