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

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 <= numberOfColumns; columnIndex ++){

alRowData.add(rs.getObject(columnIndex));

}

System.out.println(alRowData);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值