我正在尝试从记录 ID 不在列表中的数据库中选择记录。
看看下面@我的问题。
String Sqlids = "2,6,3,9"; // this is dynamic so the number of element is unknown
String str= "SELECT TOP 1 * FROM student WHERE ID NOT IN (2,6,3,9) ORDER BY NEWID()";
PreparedStatement stat = con.prepareStatement(str);
ResultSet rs = stat.executeQuery();
上述语句工作正常,但如果我将其更改为
String Sqlids = "2,6,3,9";
String str= "SELECT TOP 1 * FROM student WHERE ID NOT IN (Sqlids) ORDER BY NEWID()";
PreparedStatement stat = con.prepareStatement(str);
ResultSet rs = stat.executeQuery();
//i also try this
String Sqlids = "2,6,3,9";
String str= "SELECT TOP 1 * FROM student WHERE ID NOT IN (?) ORDER BY NEWID()";
PreparedStatement stat = con.prepareStatement(str);
stat.setString(1,Sqlids );
ResultSet rs = stat.executeQuery();
上面的语句没有过滤由于 Sqlids 是一个字符串,因此将其视为一个参数,因此它返回重复的行,是否有用于存储诸如 2,6,3,9 之类的值的整数格式?因为 Sqlids 来自一个名为 SqlidList 的数组列表,所以我尝试这样的 somtin
Iterator iTr = SqlidList.iterator();
while(iTr.hasNext()){
stat.setString(1,iTr.next().toString()+",");
}
但 setString(1,--) 不可用,因为它在一个 while 循环中