Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLFeatureNotSupportedException on getArray

Tags:

java

mysql

jdbc

Using MySQL 5.5, STS 2.9.2, mysql-connector-java-5.1.21-bin.jar

I want to get an array from ResultSet.

So I coded like this:

try {
  Connection conn = DriverManager.getConnection(url, id, pass);
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery(query); 

  if (rs.next()) {
    Array code = rs.getArray("code");
    Array count = rs.getArray("count");         

    Object objCode = code.getArray();
    Object objCount = count.getArray();

    int[] itemCode = (int[]) objCode;
    int[] itemCount = (int[]) objCount;

    // do something     
  } 

  conn.close();
  stmt.close();
  rs.close();           
} catch(SQLException e) { 
  printError(e);
}

Then, I got a SQLFeatureNotSupportedException at getArray().

If I delete the line, it does not give that exception.

I googled about it, and I found that that`s because JDBC driver does not support this method. I cannot understand what it says, and how do I solve this problem?

like image 287
Junho Kim Avatar asked Apr 28 '26 07:04

Junho Kim


1 Answers

.getArray method fetches ARRAY SQL data type. Like that: .getDecimal fetches DECIMAL SQL data type and so on... MySQL (for example) does not support ARRAY data types. So, Java reports: feature not supported.

If you want to fetch all fields as an indexed array, use .getString(1) for a VARCHAR column value at index 1.

Example query: SELECT id,title FROM news WHERE id = 1;

So you can fetch id with .getInt(1), and title with .getString(2).

That's all I can say about that ))

like image 82
RDB Avatar answered Apr 29 '26 21:04

RDB



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!