我想在SQL Query中传递增强的for循环动态值?

时间:2014-01-13 12:47:07

标签: java sql

我有一个List Collection值,如下所示,我希望在检索值时应该在sql查询中传递。

List obl = new ArrayList();
obs.add(1234);
obs.add(1235);
obs.add(1236);
obs.add(1237);

这里问题只是第一个值传递?我希望所有值都应该通过,并且应该获得适当的输出。

for(Object obls:obl){
    String sql2 ="select Student_Name,FeesHead,Amount,BankName,YearID from test where ReceiptNo = '"+obls+"'";                                             
    stat =  con.prepareStatement(sql2);
    rs=stat.executeQuery();
    rs.next();
    String StudentName  = rs.getString(1);
    String FeesHead     = rs.getString(2);
    String Amount       = rs.getString(3);
    String BankName     = rs.getString(4);
    Integer LYearID     = rs.getInt(5);
    System.out.println("Fees Head"+FeesHead);
    System.out.println("Amount"+Amount);
    System.out.println("Bank Name"+BankName);
    System.out.println("Year ID --"+LYearID);
}

4 个答案:

答案 0 :(得分:0)

试试这个:

 String sql2 = "select Student_Name,FeesHead,Amount,BankName,YearID from test where ReceiptNo = ?";
PreparedStatement stat = con.prepareStatement(sql2);

try {
   for(Object obl:obl){
   stat.setObject(1, obl);
   rs=stat.executeQuery();
   rs.next();
   String StudentName  = rs.getString(1);
   String FeesHead     = rs.getString(2);
   String Amount       = rs.getString(3);
   String BankName     = rs.getString(4);
   Integer LYearID     = rs.getInt(5);
   System.out.println("Fees Head"+FeesHead);
   System.out.println("Amount"+Amount);
   System.out.println("Bank Name"+BankName);
   System.out.println("Year ID --"+LYearID);
   }
} finally {
    if (stat != null) {
        stat.close();
    }
}

答案 1 :(得分:0)

StringBuilder sb = new StringBuilder("(");
for (Object o : obl) {
    sb.append("'").append(o).append("',");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(")");

String sql2 ="select Student_Name,FeesHead,Amount,BankName,YearID from test where ReceiptNo in " + sb.toString();
Statement stat = con.prepareStatement(sql2);
ResultSet rs=stat.executeQuery(sql2);
while (rs.next()) {
    String StudentName  = rs.getString(1);
    String FeesHead     = rs.getString(2);
    String Amount       = rs.getString(3);
    String BankName     = rs.getString(4);
    Integer LYearID     = rs.getInt(5);
    System.out.println("Fees Head"+FeesHead);
    System.out.println("Amount"+Amount);
    System.out.println("Bank Name"+BankName);
    System.out.println("Year ID --"+LYearID);
}
stat.close();
rs.close();

答案 2 :(得分:0)

    for(Object obls:obl){
        String sql2 ="select Student_Name,FeesHead,Amount,BankName,YearID from test where ReceiptNo = '"+obls+"'";                                             
        stat =  con.prepareStatement(sql2);
        rs=stat.executeQuery();
        while(rs.next())
{
        String StudentName  = rs.getString(1);
        String FeesHead     = rs.getString(2);
        String Amount       = rs.getString(3);
        String BankName     = rs.getString(4);
        Integer LYearID     = rs.getInt(5);
        System.out.println("Fees Head"+FeesHead);
        System.out.println("Amount"+Amount);
        System.out.println("Bank Name"+BankName);
        System.out.println("Year ID --"+LYearID);
}
    }

答案 3 :(得分:0)

添加到已经说过的内容: 我建议您使用if(rs.next())而不是while(rs.next()),如果您知道每个查询最多可获得一个结果。

此外,在打印出年份ID后,您可以添加以下内容:
stat.clearParameters()。
虽然在这种情况下不是绝对必要的,但它不会受到伤害(你可以阅读clearParameters以更好地了解它的使用)。