简体   繁体   English

通过套接字将JTable数据从服务器发送到客户端JTable

[英]Sending JTable data from Server to Client JTable through sockets

I have a JTable on my SERVER form, it is populated from MySQL database, coded in constructor: 我在SERVER表单上有一个JTable,它是从MySQL数据库填充的,并以构造函数编码:

String sql = "SELECT * from fiekorari";
        try{
        pst=conn.prepareStatement(sql);
        res=pst.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(res));
        pst.close();
        res.close();
        } catch (SQLException e1){
            e1.printStackTrace();
        }

Connection is Established on main using TCP connection, SERVER SIDE: 使用TCP连接(服务器端)在主服务器上建立连接:

public class Serveri extends JFrame {

    private JPanel contentPane;
    static ServerSocket ss;
    static Socket s;
    static DataInputStream din;
    static DataOutputStream dout;
    private JTextField txtMesazhi;
    private static JTextArea txtA;
    static Connection conn=null;
    static ResultSet res= null;
    static PreparedStatement pst = null;
    private Object id;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Serveri frame = new Serveri();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        SqlConn.getConnection();
        String msgin="";
        try 
        {
            ss=new ServerSocket(1201);
            s=ss.accept();
            din=new DataInputStream(s.getInputStream());
            dout=new DataOutputStream(s.getOutputStream()); //code continues with chat programming

I want to send data from JTable of Server side to JTable of Client Side, im not sure if this is the right approach to it, but my assignment is that so server will read DB data and it sends it to the client. 我想将数据从服务器端的JTable发送到客户端的JTable,我不确定这是否是正确的方法,但是我的任务是让服务器读取DB数据并将其发送给客户端。

Below is the code of Client side which establishes a connection through TCP for chatting, but now ill have to add a table which reads data from Servers table through sockets: 下面是客户端的代码,该代码通过TCP建立聊天连接,但是现在不得不添加一个表,该表通过套接字从Servers表中读取数据:

public class Klienti extends JFrame {

    private JPanel contentPane;
    private JTextField txtMesazhi;
    private static JTextArea txtShfaq1;
    static Socket s;
    static DataInputStream din;
    static DataOutputStream dout;
    private JButton btnNewButton;
    public static String sql1;
    private JTable table;
    private JTextField txtEmri;
    private JTextField txtMbiemri;
    private JTextField txtOra;
    static Connection conn=null;
    static ResultSet res= null;
    static PreparedStatement pst = null;
     private Pattern pattern;
     private Matcher matcher;
     private String loc;



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Klienti frame = new Klienti();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        //SqlConn.getConnection();
        try 
        {
            s=new Socket("localhost",1201);
            din=new DataInputStream(s.getInputStream());
            dout=new DataOutputStream(s.getOutputStream());

//code in constructor Client side which sends text message to Server through sockets: 

dout.writeUTF(msgout);
            txtShfaq1.setText(txtShfaq1.getText().trim()+"\nJu:\t"+msgout);
            txt.setText("");

No you should not send JTable from server to client. 不,您不应该将JTable从服务器发送到客户端。 Actually server should not have any swing component. 实际上,服务器不应包含任何摆动组件。 It should just fetch data on request and send just the data in a suitable format, not wrapped in a JTable. 它仅应请求获取数据,并仅发送适当格式的数据,而不包装在JTable中。

Another point, as far as possible do not do plain socket communication. 还有一点,尽量不要做普通的套接字通讯。 Use higher level protocol like HTTP. 使用更高级别的协议,例如HTTP。 Server can run a http web service and can make data available in json,xml format. 服务器可以运行http Web服务,并可以json,xml格式提供数据。

Client can use any of the many available http clients to get data from the server. 客户端可以使用许多可用的http客户端中的任何一个来从服务器获取数据。 These libraries have taken care of many of the socket connection issues. 这些库解决了许多套接字连接问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM