Home  >  Q&A  >  body text

java - preparedStatement中怎么样传如 in(?)中的参数

如下面的例子:
?中是一个列表,如'1','2','3'.
替换后应该是 in ('1','2','3'),但是我把'1','2','3'传进取却不能正确的执行取得结果,跟踪发现参数被当初一个字符串了里面的引号被转义了,变成in (''1','2','3'') ,导致结果不对,请问像sql中 in (?),preparedStatement设置参数要怎么处理?

天蓬老师天蓬老师2766 days ago681

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 10:34:48

    Just watched it again in javapreparedStatement

    should be handled like this:

    PreparedStatement statement = connection.prepareStatement("Select * from test where field in (?)");
    Array array = statement.getConnection().createArrayOf("VARCHAR", new Object[]{"A1", "B2","C3"});
    statement.setArray(1, array);
    ResultSet rs = statement.executeQuery();

    or:

    in(?,?,?)Then assign multiple times

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:34:48

    http://fish198398.iteye.com/b...

    reply
    0
  • Cancelreply