2

I work on a Java 1.7 project, with MSSQL SERVER.

I try to add an array of ids to a query:

List<String> ids = new ArrayList<String>(3);
ids.add("32123");
ids.add("4455");
ids.add("1258");
String sqlStatement = "SELECT * FROM databaseName
WHERE id IN(?)";
Connection c = null;
PreparedStatement s = null;
ResultSet rs = null;
try {
    Object[] objArr = new Object[ids.size()];
    objArr = ids.toArray(objArr);
    c = bbbDataSource.getConnection();
    s = c.prepareStatement(sqlStatement);
    Array array = c.createArrayOf("VARCHAR", objArr);
    s.setArray(1, array);
    rs = s.executeQuery();
}

The error I get is this:

Exception thrown bean: java.lang.abstractMethodError: net.sourceforge.jtds.jdbcx.proxy.ConnectionProxy.createArrayOf(LJava/lang/String:[LJava/lang/Object:)LJava/lang/Array;

What am I doing wrong?

Marc El Bichon
  • 397
  • 1
  • 7
  • 24
  • Any reason you've created an `Object[]` instead of a `String[]`? – Jon Skeet Jan 25 '17 at 09:45
  • I have the same error message with String[] – Marc El Bichon Jan 25 '17 at 09:47
  • 4
    `java.lang.AbstractMethodError` should say it all: `createArrayOf(...)` is abstract, i.e. not implemented by `net.sourceforge.jtds.jdbcx.proxy.ConnectionProxy`. That might hint at a version conflict (the classpath might contain an older version of jtds) or only part of your application having been compiled. – Thomas Jan 25 '17 at 09:48
  • 1
    Just for reference to @Thomas comment (not sure if exact this version of library is used however) : https://github.com/milesibastos/jTDS/blob/master/src/main/net/sourceforge/jtds/jdbcx/proxy/ConnectionProxy.java#L702 – rkosegi Jan 25 '17 at 09:55
  • thanks for your answers, so if I understand, I need to change this reference: net.sourceforge.jtds.jdbcx.JtdsDataSource , but to what exactly? – Marc El Bichon Jan 25 '17 at 10:04
  • What did you derive `JtdsDataSource` from? The problem is in `ConnectionProxy`. You'll either need to find a version that supports `createArrayOf()` or use a different jdbc driver. – Thomas Jan 25 '17 at 10:10

1 Answers1

1

Basically, you can't use the createArrayOf method with the jTDS library. You'll have to use a different database driver library.

Evidence (code pulled from net.sourceforge.jtds.jdbc.JtdsConnection):

/* (non-Javadoc)
 * @see java.sql.Connection#createArrayOf(java.lang.String, java.lang.Object[])
 */
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
     // TODO Auto-generated method stub
    throw new AbstractMethodError();
}

You could use something like https://learn.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sql-server since you're using SQL Server.

Another option is OscarRyz's answer of PreparedStatement with list of parameters in a IN clause

TimeDelta
  • 401
  • 3
  • 13