从数据库保留JTable中的特定数据

时间:2018-12-18 09:26:30

标签: java swing jtable

我想显示在jtable中一一保存数据的特定数据,但是从不显示所有数据,

 private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {            
    String txt = jTextField1.getText().toString();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        cn = Dataconnection.getConnection();
        qr = "SELECT p.ProductCode,p.ProductName, p.ProductPrice FROM 
       customer.product p WHERE CONCAT(p.ProductCode, p.ProductName) LIKE '%"+txt+"%'";
        ps=cn.prepareStatement(qr);
        ps.setString(1, jTextField1.getText());
        rs=ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        try {
            rs.close();
            ps.close();
        } catch (Exception e) {
        }
    }
}

0 个答案:

没有答案