【问题标题】:Navigation and search in JavaJava中的导航和搜索
【发布时间】:2023-12-21 14:01:02
【问题描述】:

我正在 Eclipse 中执行一个项目。我的导航按钮运行良好。但是,我的表单中还有一个JTable 来执行搜索,我可以显示从Jtable 到我的Jtextfields 的记录。问题是,在我执行搜索并单击任何导航按钮后,它会显示“不再记录”。

这是我的搜索代码:

txtsearch = new JTextField();
txtsearch.setBounds(723, 85, 150, 25);
add(txtsearch);
txtsearch.addKeyListener(new KeyAdapter(){
    public void keyReleased(KeyEvent e){
        String Query3 = "Select * from customer where CustCompanyName = ?";
        try {
            stt = con.prepareStatement(Query3);
            stt.setString(1, txtsearch.getText());
            rs = stt.executeQuery();
            table2.setModel(DbUtils.resultSetToTableModel(rs));                 

        } catch (SQLException e1) {
            e1.printStackTrace();
        }               
    }
});

这是我在文本字段中显示的代码:

table2.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){

        try {
            int Row = table2.getSelectedRow();
            String ccode = (table2.getModel().getValueAt(Row, 0).toString());
            String Query4 = "Select * from customer where CustomerCode = '"+ccode+"'";
            stt = con.prepareStatement(Query4);
            rs=stt.executeQuery();
            while(rs.next()) {
                txtcode.setText(rs.getString("CustomerCode"));
                txtcompname.setText(rs.getString("CustCompanyName"));
                txtaddress.setText(rs.getString("CustAddress"));
                txtpnumber.setText(rs.getString("CustPhoneNumber"));                        
            }                   
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
});

【问题讨论】:

  • '它说'...说什么?您发布的当前代码中没有任何内容定义短语“不再记录”,您的代码中也没有定义“按钮”。
  • 我只发布了在文本字段中搜索和显示的代码.. 在我的 Eclipse 中我已经声明了所有内容.. 在我的导航按钮中找到了“没有更多记录”:
  • @Aalm :你看到我的回答了吗?

标签: java swing jtable jtextfield


【解决方案1】:

你的程序逻辑没有正确实现。

多次使用相同的“rs”。

这里 table2 结果集可能有 100 行。

   String Query3 = "Select * from customer where CustCompanyName = ?";
    try {
     stt = con.prepareStatement(Query3);
     stt.setString(1, txtsearch.getText());
rs = stt.executeQuery();
     table2.setModel(DbUtils.resultSetToTableModel(rs));

这里 table2 结果集可能只有 1 行。

     String ccode = (table2.getModel().getValueAt(Row, 0).toString());
     String Query4 = "Select * from customer where CustomerCode = '"+ccode+"'";
     stt = con.prepareStatement(Query4);
rs = stt.executeQuery();

如果更改结果集rs,分配给table2的所有行,突然消失。
假设 resultset 现在只有 one row
导航按钮是多余的。

如果您不想再显示“不再记录”消息。

  • 如果只有一行,请禁用所有导航按钮,例如 nextprevious

你的 `while` 没有意义

while(rs.next()){

如果结果有很多行

为什么要一遍又一遍地填写所有JTextField()
只有最后一行/CustomerCode 会在 txtcode 中!!
一种解决方案使用 rs.next()if 而不是 while

if (rs.next()){
        txtcode.setText(rs.getString("CustomerCode"));
        txtcompname.setText(rs.getString("CustCompanyName"));
        txtaddress.setText(rs.getString("CustAddress"));
        txtpnumber.setText(rs.getString("CustPhoneNumber"));
}

在填充文本字段的代码中只有一个事件。

table2.addMouseListener(new MouseAdapter(){
   public void mouseClicked(MouseEvent e){
           ....

如果数据集光标通过next or prior 之类的导航按钮更改,您还必须有一个事件。


解决方案

  • 使用 2 个结果集
  • rsCustCompany
  • rsCustomerCode

    String Query3 = "Select * from customer where CustCompanyName = ?";
    try {
    stt = con.prepareStatement(Query3);
    stt.setString(1, txtsearch.getText());
 rsCustCompany = stt.executeQuery();
    table2.setModel(DbUtils.resultSetToTableModel(rsCustCompany));

    String ccode = (table2.getModel().getValueAt(Row, 0).toString());
    String Query4 = "Select * from customer where CustomerCode = '"+ccode+"'";
    stt = con.prepareStatement(Query4);
 rsCustomerCode = stt.executeQuery();

【讨论】: