mysql查询结果返回数组,在Java中查询数据库后返回一个数组

您所在的位置:网站首页 java返回一个数组 mysql查询结果返回数组,在Java中查询数据库后返回一个数组

mysql查询结果返回数组,在Java中查询数据库后返回一个数组

2024-07-05 09:35| 来源: 网络整理| 查看: 265

I have to query a MSSQL database and I want the result of the query to returned as a Array or ArrayList.

I have a this code now, but it gives an error.

I have a connection to the database so that's not the problem.

public ArrayList queryResult(String q) throws SQLException {

ArrayList 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.(Server.java:28) at

server.Server.main(Server.java:94) Java Result: 1

解决方案

getArray() returns the value of a particular 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 row values as array then you have to write code for that some thing like this.

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3