自动筛选JTable,一旦扫描了QR码。

huangapple go评论59阅读模式
英文:

Automatically filter JTable once QR is scanned

问题

我正在尝试使用zxing制作一个带有QR代码生成器和扫描器的考勤系统。QR部分工作正常,但我目前有一个问题。当扫描QR代码时,我希望它能自动通过文本字段对具有相同序列号的表/数据库进行过滤(我正在使用setText将QR上的文本传递到文本字段中)。

过滤的代码:

try {
    con = DriverManager.getConnection("jdbc:mysql://localhost/studentlogin", "root", "");
    String sql = "SELECT * FROM studentqrlogin WHERE " + "SN like '%" + tfSearch.getText() + "%'";
    pst = con.prepareStatement(sql);
    rs = pst.executeQuery();

    jtTable.setModel(DbUtils.resultSetToTableModel(rs));

} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

到目前为止,我在按键释放事件上有这段代码。我还尝试将其添加到其他事件侦听器中,但它不像我希望的那样工作。

英文:

I am trying to make an attendance system with QR generator and scanner using zxing. QR part is working fine but I have one problem right now. When the QR code get scanned, I want it to automatically filter the table/database with the same serial number through textfield (I am using setText to pass the text on QR to textfield.

Code for Filter:

 try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/studentlogin", "root", "");
            String sql = "SELECT * FROM studentqrlogin WHERE " + "SN like '%" + tfSearch.getText() + "%'";
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();

            jtTable.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }  

So far, I have this code on a key released event. I also tried adding it to other event listener but it doesn't work like I want it to be.

答案1

得分: 2

@Override  
public void changedUpdate(DocumentEvent e) {
    search();
}
@Override
public void removeUpdate(DocumentEvent e) {
    search();
}
@Override
public void insertUpdate(DocumentEvent e) {
    search();
} 

public void search() {
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost/studentlogin", "root", "");
        String sql = "SELECT * FROM studentqrlogin WHERE " + "SN like '%" + jftfSearch.getText() + "%'";
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();
        // String sql = "SELECT No, Title, Author, Genre, Lexile, Points FROM LibrarySystemDatabase WHERE "
        //  + "No like '%" + txtSearch.getText() + "%'";

        jtTable.setModel(DbUtils.resultSetToTableModel(rs));
       // updateTable();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }    
}
});
英文:

Netbeans 8.2 (IDE I am currently using) does not have any event for document listener so adding it on code customizer helps.

@Override  
    public void changedUpdate(DocumentEvent e) {
   search();
  }
  @Override
  public void removeUpdate(DocumentEvent e) {
    search();
  }
   @Override
  public void insertUpdate(DocumentEvent e) {
   search();
  } 
  
  public void search() {
     try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/studentlogin", "root", "");
            String sql = "SELECT * FROM studentqrlogin WHERE " + "SN like '%" + jftfSearch.getText() + "%'";
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            // String sql = "SELECT No, Title, Author, Genre, Lexile, Points FROM LibrarySystemDatabase WHERE "
            //  + "No like '%" + txtSearch.getText() + "%'";

            jtTable.setModel(DbUtils.resultSetToTableModel(rs));
           // updateTable();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }    
    }
});

huangapple
  • 本文由 发表于 2020年10月3日 22:21:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定