ps = conn.prepareStatement("SELECT tid,jdp_response FROM jdp_tb_trade WHERE tid IN (?) ORDER BY created DESC");
ps.setString(1, "3152892500240988,3152893300713891");
本人如上使用 prepareStatement 结果只能查询出来一条数据,也不报错,调试了一天啊!!!尝试了各种办法 都没成功。后来突发奇想知道原因了;我如上设置,最终执行的sql语句可能是:
SELECT tid,jdp_response FROM jdp_tb_trade WHERE tid IN ('3152892500240988,3152893300713891') ORDER BY created DESC
而我想真正执行的SQL语句是:
SELECT tid,jdp_response FROM jdp_tb_trade WHERE tid IN (3152892500240988,3152893300713891) ORDER BY created DESC
那么由于in()里 有多个参数,所以需要多个?号 多个setLong才行。即in(?,?,?.....)
怎么办呢;
看了很多网上的做法,我自己写出了我自己的写法如下:
String tids = "3152892500240988,3152893300713891"; String[] tidsAry = tids.split(","); StringBuffer stringBuffer = new StringBuffer("?"); for (int i = 0; i <= tidsAry.length - 2; i++) { stringBuffer.append(",?"); } System.out.println(stringBuffer.toString()); ps = conn.prepareStatement("SELECT tid,jdp_response FROM jdp_tb_trade WHERE tid IN (".concat(stringBuffer.toString()).concat(") ORDER BY created DESC")); System.out.println(tidsAry.length); for (int i = 0; i <= tidsAry.length - 1; i++) { System.out.println(i); ps.setLong(i + 1, Long.parseLong(tidsAry[i])); } ResultSet rs = ps.executeQuery(); while (rs.next()){ System.out.println(rs.getString(1)); }