我有JDBC表,其中包含大量记录,我想将所选查询中的结果集存储到多维数组中,并在jsp页面中显示为表。
Servlet页面:
ArrayList al = new ArrayList();
ArrayList list = new ArrayList();
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@ip/test","user","pass");
} catch (SQLException e) {
out.println(e.getMessage());
}
try {
OraclePreparedStatement stmt
= (OraclePreparedStatement) con.prepareStatement(
"Select * from MSG");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
}
答案 0 :(得分:0)
我没有测试过,但基本上是:
a)crate ArrayList<ArrayList<Object>>
来存储结果。
b)获取列数rs.getMetaData().getColumnCount()
c)对于每个行包ArrayList<Object>
,从行迭代colums并将其添加到其中。
d)将行添加到结果数组中。
基于您的代码,它可能是这样的:
ArrayList<ArrayList<Object>> resultTable = new ArrayList<ArrayList<Object>>();
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); con = DriverManager.getConnection("jdbc:oracle:thin:@ip/test","user","pass");
} catch (SQLException e) {
out.println(e.getMessage());
}
try {
OraclePreparedStatement stmt
= (OraclePreparedStatement) con.prepareStatement(
"Select * from MSG");
ResultSet rs = stmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
ArrayList<Object> row = new ArrayList<Object>();
for (int col=1; col < columnCount) {
row.add(rs.getObject(col));
}
resultTable.add(row);
}
} catch ...
// here you have results rewriten to resultsTable