I'm using this:
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());
Searching using the same query works fine on the MySQL terminal. It's only when using PreparedStatement
in Java
that it doesn't.
rowIdCombo is basically a string of numbers with comma separated values. Something like this: 23,56,64,3
.
The result set returned is empty. How do I get this query to work?
Based on the output of
System.out.println(preStatement);
which was:
com.mysql.jdbc.PreparedStatement@223d2c72: SELECT * FROM mydb.SomeTable WHERE attributeId = '6' AND substring_index(substring_index(rowIdCombo,',',2),',',-1) = '1'
and as per your comment, that replacing = '1' with = 1 have solved the issue, to prevent the single quotes wrapping, set the value in the PreparedStatement
as integer, use this:
preStatement.setInt(2, Integer.parseInt(locationID.toString()));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.