我正在使用DatabaseMetaData实例检查数据库。我获得了DB中表格的所有信息,并且我在没有任何问题的情况下迭代所有Resultset。
在迭代结束时,我想返回ResultSet的开头,所以我调用beforeFirst()方法,然后调用next()来获取ResultSet的第一个元素。这是我的代码:
connect(request.getParameter("source"));
DatabaseMetaData patrol = link.getMetaData();
answer = patrol.getTables(null, null, null, null);
while (answer.next()) {
String nomTable = answer.getString("TABLE_NAME");
System.out.println(nomTable)
}
answer.beforeFirst();
answer.next();
String table = answer.getString("TABLE_NAME");
answer.close();
我已经获得了所有结果但是我有这个例外:
java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY
at sun.jdbc.odbc.JdbcOdbcResultSet.beforeFirst(Unknown Source)
at InspectDB.doPost(InspectDB.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:747)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155
)
at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155
)
at com.sun.web.core.Context.handleRequest(Context.java:414)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:139)
'直到这里一切似乎都很正常。根据我的连接(JDBC-OBDC到MSAcsess),我的ResultSet的获取模式是ONLY_FORWARD,所以我尝试使用
更改它answer.setFetchDirection(ResultSet.FETCH_REVERSE);
answer.beforeFirst();
answer.next();
为了允许预约模式...但它不允许我。新例外:
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcResultSet.setFetchDirection(Unknown Source)
at InspectDB.doPost(InspectDB.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:747)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155)
at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155)
at com.sun.web.core.Context.handleRequest(Context.java:414)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:139)
是否可以将获取模式设置为来自DatabaseMetaData的Resultset?我该怎么办?
感谢。
答案 0 :(得分:1)
ResultSet对象支持的不同提取类型可能因实现和您要查询的数据库而异。通过设置获取方向或尝试在第一个项之前重置ResultSet(尽管setFetchDirection应该抛出SQLException而不是NPE),无法在FORWARD_ONLY结果集中滚动两次。来自ResultSet API。
void setFetchDirection(int direction)
throws SQLException
Gives a hint as to the direction in which the rows in this ResultSet object will be processed. The initial value is determined by the Statement object that produced this ResultSet object. The fetch direction may be changed at any time.
Parameters:
direction - an int specifying the suggested fetch direction; one of ResultSet.FETCH_FORWARD, ResultSet.FETCH_REVERSE, or ResultSet.FETCH_UNKNOWN
Throws:
SQLException - if a database access error occurs; this method is called on a closed result set or the result set type is TYPE_FORWARD_ONLY and the fetch direction is not FETCH_FORWARD
Since:
1.2
See Also:
Statement.setFetchDirection(int), getFetchDirection()
处理需要多次迭代的ResultSet的标准方法是迭代一次并将其复制到List中。然后,您可以根据需要安全地滚动列表。