2

Hi am just selecting Data from SQL SERVER and stroing it in Result set. I am getting below error.

**JDBC ERROR : AbstractMethodError: com.microsoft.sqlserver.jdbc.SQLServerConnection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array**

The problem with this below line

    java.sql.Array sqlarray = sqlcon.createArrayOf(null, rowValues.toArray());

I am not sure why this issue occurs and what exact parameter should I pass createArrayOf().. Please help for this below issue

Here is full my code

    public void getDatafromsql(ArrayList rowValues) throws SQLException,ClassNotFoundException 
    {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    System.out.println("SQL Server Start Connecting ");
    Connection sqlcon = DriverManager
            .getConnection(s_url, s_username, s_password);
    System.out.println("Sql server Connection valid");
    System.out.println(rowValues);
    PreparedStatement s1 = sqlcon.prepareStatement("SELECT TOP 6 myid,document,name FROM table1 where DATEMODIFIED between ? and ? and myid in (?)");
    s1.setTimestamp( 1,V_DOC_LAST_REFRESH_TIMESTAMP);
    s1.setTimestamp( 2,V_DOC_CURRENT_REFRESH_TIMESTAMP);
    //Below line throwing error
    java.sql.Array sqlarray = sqlcon.createArrayOf(null, rowValues.toArray());
    s1.setArray(3, sqlarray);
    sql_rs = s1.executeQuery();
    setdataOracle(sql_rs);
    System.out.println("SQL Server Storage Completed ");
    sqlcon.close();
    }
2
  • 2
    Which JDBC driver version are you using. AbstractMethodError is indicating an unimplemented method Commented Dec 23, 2014 at 14:00
  • Does SQL Server even support arrays? Commented Dec 23, 2014 at 14:46

3 Answers 3

1

The SQL Server JDBC driver doesn't support that method:

public java.sql.Array createArrayOf(String typeName,

        Object[] elements) throws SQLException {

    // Not implemented

    throw new SQLFeatureNotSupportedException(SQLServerException.getErrString("R_notSupported"));

}

https://github.com/Microsoft/mssql-jdbc/blob/master/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

Instead use Table Valued Parameters, or pass the data as XML, JSON, or CSV and parse it on the server.

Sign up to request clarification or add additional context in comments.

Comments

0

Did you tried call

sqlcon.createArrayOf("INT", rowValues.toArray());

or

sqlcon.createArrayOf("integer", rowValues.toArray());

or some data type your data base system supports. If nothing works, probably the driver you use does not support arrays. In this case create placeholders (?) "hard coded" and use

s1.setInt(i, intValue);

in a loop (where i is 3 to (array size + 3)) instead of

s1.setArray(3, sqlarray);

Comments

0

Example how to use table valued parameters as a solution for SQL Server since createArrayOf() is not supported yet.

private void createTableType() throws SQLException, IOException, ClassNotFoundException {
    try(Statement stmt = DBUtils.dbConnect().createStatement()){
        stmt.executeUpdate("IF NOT EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = 'Codes' ) " +
                "BEGIN CREATE TYPE dbo.Codes AS TABLE (Code VARCHAR(25)) END");
    }catch (ClassNotFoundException | SQLException e){
        if(e instanceof SQLException){
            DBUtils.printSQLException((SQLException) e);}
        e.printStackTrace();
        throw e;
    }
}

public void tableValuedParamQuery(List<String> params) throws IOException, SQLException, ClassNotFoundException {
    try {
        createTypeTable();
        SQLServerPreparedStatement ssps = (SQLServerPreparedStatement) dbConnect().prepareStatement(
                "SELECT * FROM dbo.Products p" +
                     "INNER JOIN ? AS c ON c.Code = p.Code"
        );
        SQLServerDataTable dt = new SQLServerDataTable();
        dt.addColumnMetadata("Codes", Types.VARCHAR);
        params.forEach(e -> {
            try {
                dt.addRow(e);
            } catch (SQLServerException ex) {
                ex.printStackTrace();
            }
        });
        ssps.setStructured(1, " dbo.Codes", dt);
        ResultSet rs = ssps.executeQuery();
    }catch (ClassNotFoundException | SQLException e){
        if(e instanceof SQLException){
            DBUtils.printSQLException((SQLException) e);}
        e.printStackTrace();
        throw e;
    }
}

Comments

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.