将表数据与字符串进行比较

时间:2014-03-03 00:22:15

标签: java mysql sql

我正在尝试查看来自我的数据库的数据是否与用户输入匹配。我已经尝试过这段代码,但它没有做我需要做的事情。我希望它显示一条消息,说明它们是否匹配。

 try {
        String find = BC.getText(); //Get text from Textfield
        String sql = "select * from Inventory where Barcode=?";
        st = con.prepareStatement(sql);
        rs = st.executeQuery();

        while (rs.next()) {
            if("Barcode".equals(find))
            {
              JOptionPane.showMessageDialog(null,"Matching");  

            }   
            else JOptionPane.showMessageDialog(null,"not matching");   
        }

    } catch (Exception ex) {


    }

1 个答案:

答案 0 :(得分:0)

输入您要搜索的参数。参见;

 String find = BC.getText(); //Get text from Textfield
 String sql = "select * from Inventory where Barcode=?";
 st = con.prepareStatement(sql);
 st.setString(1, "12345678");//Set Barcode value.
 rs = st.executeQuery();