从数据库中搜索

时间:2012-12-30 14:36:25

标签: java sql

private void btgetinvActionPerformed(java.awt.event.ActionEvent evt) {
    if(tf_rmid.getText().length()==11){
        JOptionPane.showMessageDialog(null,"REMITTANCE ID IS VALID!");
        try {
                DBUtil util = new DBUtil();
                Connection con = util.getConnection();
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("select bk_det.rm_id from bk_det inner join bk_rep on bk_det.rm_id = bk_rep.rm_id WHERE rm_id = ?");
                String rm = tf_rmid.getText().trim();          
                stmt.setString(1, ""+(rm));
                while(rs.next()){
                   int i = rs.getString("RM ID");
                   String fn = rs.getString("First Name");
                   String ln = rs.getString("Last Name");
                   String title = rs.getString("Title");
                   String city = rs.getString("City");
                   txtFirstName.setText(fn);
                   txtLastName.setText(ln);
                   txtTitle.setText(title);
                   txtCity.setText(city);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
            }
    }else{
       JOptionPane.showMessageDialog(null,"PLEASE ENTER VALID REMITTANCE ID!");
    }

}

我正在尝试使用select语句从我的数据库中搜索值,但是当我尝试获取值时?对于where选项我收到错误我认为它的语法错误可以ny一个请帮助???

3 个答案:

答案 0 :(得分:1)

执行此操作,

select bk_det.rm_id from bk_det inner join bk_rep 
on bk_det.rm_id = bk_rep.rm_id WHERE bk_det.rm_id = ?

可能你在where子句

中缺少表名

答案 1 :(得分:1)

将参数设置为执行查询后的语句似乎有点奇怪。

另外

int i = rs.getString(“RM ID”);

这不编译(如果rs是java.sql.ResultSet实例)

答案 2 :(得分:1)

使用准备好的声明:

    String query = "select bk_det.rm_id from bk_det inner join bk_rep on bk_det.rm_id = bk_rep.rm_id WHERE rm_id = ?";
    try {
        PreparedStatement preps = con.prepareStatement(query);
        preps.setString(1, ""+(rm));
        preps.execute();
        ...