7

I am facing error java.sql.SQLFeatureNotSupportedException in my prepare statement. I am using Mysql database.

Below is my code.

class tmp {
public static void main(String arg[]) {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost/sample", "root", "root");
        PreparedStatement pst = conn
                .prepareStatement("select * from userinfo where firstname in(?)");

        String[] Parameter = { "user1", "Administrator" };
        Array sqlArray = conn.createArrayOf("VARCHAR", Parameter);
        pst.setArray(1, sqlArray);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
        }
    }
}

3 Answers 3

9

For Mysql -

Setting array is not possible in Mysql.

Instead of that you can form a query for (?,?,..) in the loop and same way for setting values.

String[] Parameter = { "user1", "Administrator" };
String query = "select * from userinfo where firstname in (";
String temp = "";

for(i = 0; i < Parameter.length; i++) {
  temp += ",?";
}

temp = temp.replaceFirst(",", "");
temp += ")";
query = query + temp;

PreparedStatement pst = conn.prepareStatement(query);

so query becomes

select * from userinfo where firstname in (?,?)

and pass values also using loop.

For Oracle -

ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn);
String[] Parameter = { "user1", "Administrator" };
java.sql.Array sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content);
.
.
pstmt.setArray(1, sqlArray);
Sign up to request clarification or add additional context in comments.

5 Comments

Your answer is for MySQL or Oracle?
It gives me the error "java.lang.ClassCastException: com.mysql.jdbc.JDBC4Connection cannot be cast to oracle.jdbc.OracleConnection"
Sorry that was for oracle.
how can i do it with Mysql
I have edited my answer..create (?,?...) programatically
4

Error message is very clear. And MySQL does not support custom data types.

Currently MySQL is supporting only:

  1. Numeric Type
  2. Date and Time Type
  3. String Type

Or, you can use each of the input values as a set of values of IN function in MySQL.

Change your JAVA code as follows:

StringBuilder sbSql = new StringBuilder( 1024 );
sbSql.append( "select * from userinfo where firstname in(" );

for( int i=0; i < Parameter.length; i++ ) {
  if( i > 0 ) sbSql.append( "," );
  sbSql.append( " ?" );
} // for
sbSql.append( " )" );
PreparedStatement pst = conn.prepareStatement( sbSql.toString() );

for( int i=0; i < Parameter.length; i++ ) {
  pst.setString( i+1, Parameter[ i ] );
} // for

ResultSet rs = pst.executeQuery();

5 Comments

How are you storing values in firstname column. What is its data type?
The datatype is string
by taking the multiple value or single parameter
You mean to say that user1 and Administrator can go as two different records?
Ok. You have an answer edited corrected. You can follow that.
-1

Convert List to a comma separated String and use it.

Class Tmp {
        public static void main(String arg[]) {

            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost/sample", "root", "root");

                // Consider this list is already constructed
                List<String> parameter = new ArrayList<String>();
                parameter.add("user1");
                parameter.add("Administrator");

                String parameterStr = "'" + String.join("','", parameter) + "'";
                PreparedStatement pst = conn.prepareStatement("select * from userinfo where firstname in(" + parameterStr + ")");

                ResultSet rs = pst.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getInt(1));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

3 Comments

Will it be sql injection?
yes, it leads to sql injection.
this leads to sql injection which defeats the purpose of the prepared statement which op needed. i have no idea how this answer passes the review.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.