Passing array parameter in prepare statement - getting “java.sql.SQLFeatureNotSupportedException”

后端 未结 3 769
逝去的感伤
逝去的感伤 2020-12-16 08:39

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

Below is my code.

class tmp {
public s         


        
相关标签:
3条回答
  • 2020-12-16 09:02

    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();
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-16 09:03

    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();
    
    0 讨论(0)
  • 2020-12-16 09:05

    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);
    
    0 讨论(0)
提交回复
热议问题