验证数据库中的AdminNo

时间:2017-08-05 20:20:02

标签: java sql

我有一个文本框,它接受输入并验证数据库中是否有记录。如果没有记录,则应显示JOptionPane。但是,如果我输入任何不在数据库中的随机数并按下检查按钮,则没有任何反应。如果我输入数据库中的adminNo,则检查按钮将起作用。我该如何解决?我认为它更多的是我在代码中的逻辑

JButton btnCheck = new JButton("Check");
    btnCheck.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                    try{
                    String conn = "select instrument, period, adminNo from reservation "
                            + "where adminNo = '" + txtAdmin.getText() + "'"; 
                    PreparedStatement pst = sqliteConnection.dbConnector().prepareStatement(conn);
                    ResultSet rs = pst.executeQuery();

                    while(rs.next()) {
                        String adminNo = rs.getString("adminNo");
                        if(txtAdmin.getText().equals(adminNo)){
                            System.out.println("Admin number from text: " + txtAdmin.getText() + "Admin number from DB: " + adminNo);
                        lblDisplayMusicalInstrument.setText(rs.getString("instrument"));
                        lblDisplayDuration.setText(rs.getString("period"));
                }else{
                    JOptionPane.showMessageDialog(null, "Admin not in database");
                }
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                }

        }
    }

);      

1 个答案:

答案 0 :(得分:0)

尝试以下代码

<Resources cachingAllowed="true" cacheMaxSize="100000" />