【问题标题】:SQL query shows only one result (the first one) but not all the result [duplicate]SQL查询只显示一个结果(第一个),但不是所有结果[重复]
【发布时间】:2017-01-19 18:16:45
【问题描述】:
    private void search_fKeyReleased(java.awt.event.KeyEvent evt) {                                     
    try {

这里有什么问题

        PreparedStatement pst =null;
        ResultSet rst=(ResultSet) pst;
        Connection con=(Connection)  
        DriverManager.getConnection("jdbc:mysql://localhost/iqari", 
        "root","");


        String sql="select * from first where  masaha_iqar=?";
        pst=(PreparedStatement) con.prepareStatement(sql);
        pst.setString(1,search_f.getText());
        rst=pst.executeQuery();

在文本区域中,我得到的是结果,而不是我的数据库中可用的两个

        if (rst.next()){String add1=rst.getString("raqm_iqar");
        jTextArea2.append(add1 + "\n");

        System.out.format("%s",add1);
        }

    } catch (Exception e) {
    }
  TODO add your handling code here:
}

【问题讨论】:

  • 用while循环代替if。
  • 这对 SMA 很有帮助

标签: java mysql search


【解决方案1】:
try {
    PreparedStatement pst =null;
    ResultSet rst=(ResultSet) pst;

    Connection con=(Connection)    DriverManager.getConnection("jdbc:mysql://localhost/iqari", "root","");
    String sql="select * from first where  masaha_iqar=?";
    pst=(PreparedStatement) con.prepareStatement(sql);
    pst.setString(1,search_f.getText());
    rst=pst.executeQuery();
   while (rst.next()){String add1=rst.getString("raqm_iqar");
    jTextArea2.append(add1 + "\n");
    System.out.format("%s",add1);
    }

} catch (Exception e) {
}

【讨论】:

  • 非常感谢你救了我的命 Rakesh :)
猜你喜欢
  • 2011-07-26
  • 1970-01-01
  • 2021-02-01
  • 2017-12-12
  • 2015-03-16
  • 2021-02-24
  • 2021-11-01
  • 2015-08-02
  • 2012-05-13
相关资源
最近更新 更多