Array return after database query in Java

I need to query the MSSQL database, and I want the query result to be returned as Array or ArrayList.

I have this code now, but it gives an error. I have a database connection so no problem.

public ArrayList<Array> queryResult(String q) throws SQLException {

   ArrayList<Array> array = new ArrayList<>();
   Statement statement = this.getConnection().createStatement();
   ResultSet rs = statement.executeQuery(q);

   while(rs.next()) {

      Array n = rs.getArray(rs.getRow());
      System.out.println(n);
      array.add(n);

   }
   return array;
}

I get the following error

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: This operation is not supported.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.NotImplemented(SQLServerResultSet.java:750)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getArray(SQLServerResultSet.java:2625)
      at server.Database.queryResult(Database.java:52)
      at server.Server.listen(Server.java:57)
      at server.Server.run(Server.java:34) at
      server.Server.<init>(Server.java:28) at
      server.Server.main(Server.java:94) Java Result: 1
+3
source share
2 answers

getArray()returns the value of a specific column of the current row as an array. See This http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getArray%28int%29

If you want to get the string values ​​as an array, then you need to write code for this thing.

while (rs.next()){
     java.util.ArrayList alRowData = new java.util.ArrayList();
     java.sql.ResultSetMetaData rsmd = rs.getMetaData();
     int numberOfColumns = rsmd.getColumnCount();
     for(int columnIndex = 1; columnIndex <= numberOfColumns; columnIndex ++){
          alRowData.add(rs.getObject(columnIndex));
     }
     System.out.println(alRowData);
}
+4
source

Your exception trace shows that:

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: This operation is not supported.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.NotImplemented(SQLServerResultSet.java:750)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getArray(SQLServerResultSet.java:2625)

, SQLDriver getArray(int) , , .

, .

:
Row Record ResultSet, getRow() getArray(..) , AFAIK .

+1

All Articles