I've been trying to select values from MySQL in Java where var IN
an array, i've been reading other answers for the past 3 hours, some of them have marked accepted answers, but non of these answers solved the problem for me I tried many examples, but I keep getting the same exception :
Exception in thread "main" java.sql.SQLFeatureNotSupportedException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLFeatureNotSupportedException(SQLError.java:1172)
at com.mysql.jdbc.JDBC4Connection.createArrayOf(JDBC4Connection.java:52)
at aero.Connecting.search(Connecting.java:150)
Where Connecting.java:150 = java.sql.Array sqlArray = c.createArrayOf("bigint", data);
(same problem with int
and VARCHAR
too)
Nothing I tried so far is working, some answers like this say that this is not possible to do it the way I want it (one dynamic array) but I can't accept this claim since many other answers say it is possible.
Here is what i've tried so far:
Version 1:
public String createInListPattern(int n) {
return StringUtils.repeat("?", ",", n);
}
Connection c =get_conn();
PreparedStatement s = c.prepareStatement("SELECT ID FROM users WHERE ID in ("+createInListPattern(2)+")");
boolean status=false;
int size=2;
Integer[] data = new Integer[2];
data[0]=13;
data[1]=14;
java.sql.Array sqlArray = c.createArrayOf("bigint", data);
s.setArray(1, sqlArray);
for (int i = 0; i < size; i++) {
s.setInt(i + 1, data[i]);
}
ResultSet rs =s.executeQuery();
if(rs.next()) {
System.out.println(rs.getInt("ID"));
}
Version 2:
Connection c =get_conn();
PreparedStatement s = c.prepareStatement("SELECT ID FROM users WHERE ID in (?)");
boolean status=false;
Object[] data = new Object[2];
data[0]=13;
data[1]=14;
java.sql.Array sqlArray = c.createArrayOf("bigint", data);
s.setArray(1, sqlArray);
ResultSet rs =s.executeQuery();
if(rs.next()) {
System.out.println(rs.getInt("ID"));
}
In addition to the above versions, I tried some of the following:
- SO question 1
- SO question 2 (My JDBC jar version is 5.x)
- SO question 3
As i mentioned before, the exception I've been receiving is the same in all attempts so far and seems to be coming from the same line ( c.createArrayOf("bigint", data)
).
Any working solutions?