1

我正在使用 DatabaseMetaData 实例检查数据库。我获得了数据库中表的所有信息,并毫无问题地迭代了所有结果集。

在迭代结束时,我想回到 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)

'直到这里一切看起来都很正常。根据我的连接(到 MSAcsess 的 JDBC-OBDC),我的 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 的结果集?我该怎么做??

谢谢。

4

1 回答 1

1

ResultSet 对象支持的不同获取类型可能因实现和您查询的数据库而异。不可能通过 FORWARD_ONLY 结果集滚动两次,方法是设置获取方向或尝试在第一项之前重置 ResultSet(尽管 setFetchDirection 应该抛出 SQLException 而不是 NPE)。来自 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 中。然后,您可以根据需要安全地滚动列表多次。

于 2011-02-08T21:09:54.530 回答