[英]Query parameter not recognized on PreparedStatement when used in combination with substring_index()
[英]PreparedStatement not working for substring_index?
我正在使用这个:
PreparedStatement preStatement = conn.prepareStatement("SELECT * FROM SomeTable WHERE attributeId = ? AND substring_index(substring_index(rowIdCombo,',',2),',',-1) = ?");
preStatement.setString(1, anAttributeID.toString());
preStatement.setString(2, locationID.toString());
使用相同的查询进行搜索在MySQL终端上可以正常工作。 只有在Java
中使用PreparedStatement
时才这样做。
rowIdCombo基本上是带有逗号分隔值的数字字符串。 像这样: 23,56,64,3
。
返回的结果集为空。 如何使该查询起作用?
根据输出
System.out.println(preStatement);
那是:
com.mysql.jdbc.PreparedStatement@223d2c72: SELECT * FROM mydb.SomeTable WHERE attributeId = '6' AND substring_index(substring_index(rowIdCombo,',',2),',',-1) = '1'
和按您的评论,与= 1已经解决了这个问题,防止单引号替换包装=“1”,在设定值PreparedStatement
为整数,使用:
preStatement.setInt(2, Integer.parseInt(locationID.toString()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.