Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with mysql JDBC driver for method createArrayOf

I'm a begginer java programmer. In my dummy projects I'm using the mysql JDBC 5.17 driver for connect to my database

Today I have a simply query

SELECT * FROM books WHERE idb IN (?)

I have this Array of int:

int[2] idBooks = new int{1,2};

and I would to select the books with the id in idBooks

When I use the API "createArrayOf" the JVM throws on the console this error:

  java.sql.SQLFeatureNotSupportedException

I know that my driver not support the "createArrayOf" method, there are other solution for do that?

I hope my question is not trivial :)

Thanks a lot in advance, sorry for my english Thanks to all!!

like image 411
Andrea Catania Avatar asked Aug 24 '26 15:08

Andrea Catania


1 Answers

One possible solution:

Java:

// str_idBooks will be [1, 2]
String str_idBooks = Arrays.toString( idBooks );

You have to replace '[', ' '(spaces), ']' from the string to pass to MySQL query.

MySQL:

If you did not replace the characters as said above, use this statement:

SELECT * FROM books
 WHERE FIND_IN_SET( idb, 
                    replace(replace(replace(?,'[',''),']',''),' ','')
                  ) > 0

If you have replaced the characters as said above, use this statement:

SELECT * FROM books
 WHERE FIND_IN_SET( idb, ? ) > 0

Use the Prepared Statement to set the parameter as :

pst.setString( 1, str_idBooks );
like image 98
Ravinder Reddy Avatar answered Aug 26 '26 03:08

Ravinder Reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!