0
Follow
0
View

java swing Jcombox

gsdhdsz 注册会员
2023-02-25 22:18

Hello there is a question about jtable updating to display the latest data in the database. You can do this :
First of all, set up a thread or timer, periodically query the latest data from the database
Second, you can use the largest id of the current data as the query condition, query whether the data is larger than this id, if there is a new data. Then look it up.
Again, for the new data, you can only query the updated data and then add it to jtable, but you need to dynamically add rows of jtable, or query all the current data and put it into jtable. In this way, you do not need to consider dynamically adding rows of jtable.

djbm71411 注册会员
2023-02-25 22:18

Part of the answer references GPT, GPT_Pro to better solve the problem
To implement this feature, first need to prepare a Jcombobox in Swing, Then we need to set up a listening event, which is used to monitor whether the content in Jcombobox changes. Once it changes, we will perform some actions, such as finding the ip address selected in Jcombobox from the database and displaying it in Jtable.

We can use java.sql.Connection, java.sql.Statement, java.sql.ResultSet and other classes to connect to the database, and realize the database query.

The specific code implementation can be divided into two steps:
  1. Monitor content changes in Jcombobox

    //定义一个Jcombobox
    JComboBox comboBox=new JComboBox(); 
    //往Jcombobox中放入ip地址的选项
    comboBox.addItem("显示全部"); 
    comboBox.addItem("192.168.01"); 
    comboBox.addItem("192 168.02"); 
    comboBox.addItem("192 168.03"); 
    //新建一个监听事件,当Jcombobox的选项发生变化的时候就触发这个事件
    comboBox.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
         //TODO:在这里添加你要执行的代码 
     } 
    }); 
    
  2. Executes database query code in listening event

    //获取当前Jcombobox中选中的ip地址 
    String ipAddress=comboBox.getSelectedItem().toString(); 
    //如果是显示全部,则不用进行数据库查询,而是直接把数据库中所有的行显示到Jtable中去 
    if(!"显示全部".equals(ipAddress)){ 
     //新建一个Connection对象,用于连接数据库 
     Connection conn=DriverManager.getConnection(url, username, password); 
    
     //定义sql语句,根据ip地址来查询table表里的内容 
     String sql="select * from table where ipaddress=?"; 
    
     //新建一个PreparedStatement对象,用于执行sql语句 
     PreparedStatement pstmt=conn.prepareStatement(sql); 
    
     //将ip地址赋值到sql语句中 
     pstmt.setString(1, ipAddress); 
    
     //执行sql语句,并将结果集存储到ResultSet对象中去 
     ResultSet rs=pstmt.executeQuery(); 
    
     //新建一个DefaultTableModel对象,用于存储从数据库中获得的数据 
     DefaultTableModel dtm=new DefaultTableModel(); 
    
     //将ResultSet对象里的数据存储到DefaultTableModel对象里去 
     while (rs.next()){ 
         Vector v = new Vector();   //新建一个Vector对象用于存储一行数据  
    
         //将ResultSet里的id、ip地址、数据依此存储到Vector对象里去  
         v.add(rs.getString("id"));  
         v.add(rs.getString("ipaddress"));  
         v.add(rs.getString("data"));  
    
         //将Vector对象存储到DefaultTableModel里去  
         dtm.addRow(v);  
    
     }  
    
     //将DefaultTableModel对象作为参数传递给Jtable  
     jtable.setModel(dtm);  
    
     //关闭ResultSet、PreparedStatement、Connection对象  
     rs.close();  
     pstmt.close();  
     conn.close();  
    }else{  
      //TODO:如果是显示全部,则要将数据库中所有的行显示到Jtable中去  
    }  
    

    If the answer is helpful, please accept it.

dzfu123 注册会员
2023-02-25 22:18

First query the record to receive the collection, and then render to the Jcombox object on the line.

About the Author

Question Info

Publish Time
2023-02-25 22:18
Update Time
2023-02-25 22:18

Related Question

java动态生成pdf报表

带有空值的JSON_MERGE_PATCH(Javascript)

求解决个正则问题,java,正则

java字符串部分匹配问题

在java里面如何使用selenium的模块对浏览器进行操作

在其他ul li Javascript中获取ul li元素

添加一个编译器来运行java代码并显示输出

将Derby连接到java

最近的爱心代码,可以用Java但是用c一直报错

Java抽象类和接口