我可以更改数据库元数据查询结果集中的获取模式吗?

发布于 2024-10-16 08:43:30 字数 2438 浏览 2 评论 0原文

我正在使用 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)

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

谢谢。

I'm inspecting a Database using an DatabaseMetaData instance. I get all the information of the tables in the DB and I iterate all the Resultset without anyproblem.

At the end of the iteration, I want to return to the begin of the ResultSet, so I call the beforeFirst() method followed by the next() to get the first element of the ResultSet. Here's my code:

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();

I've got all my results but then I've got this exception:

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)

'Till here everything seems normal. According to my connection (a JDBC-OBDC to a MSAcsess), my ResultSet's fetch mode is ONLY_FORWARD, so I tried to change it using

answer.setFetchDirection(ResultSet.FETCH_REVERSE);
answer.beforeFirst();
answer.next();

in order to allow the reserve mode... but it doesn't allow me. New exception:

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)

Is it possible to set the fetch mode to a Resultset comming from a DatabaseMetaData? How can I do it??

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2024-10-23 08:43:30

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

The different fetch types supported by a ResultSet object can vary depending on implementation and the database you're querying. It is not possible possible to scroll twice through a FORWARD_ONLY resultset, either by setting the fetch direction or by trying to reset the ResultSet before the first item(although setFetchDirection should throw an SQLException not an NPE). From the 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()

The standard way of dealing with a ResultSet that you need to iterator over multiple times is to iterate through it once and copy it into a List. You can then scroll through the list safely as many times as you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文