gpt4 book ai didi

java - 如果结果集不可滚动,则绝对函数不应该工作

转载 作者:行者123 更新时间:2023-12-02 02:56:22 24 4
gpt4 key购买 nike

嗨,我是 JDBC 概念的新手,根据我的知识,要在两个方向上移动结果集指针,需要将结果集设置为可滚动

但是由于结果集未设置为可滚动绝对函数不应允许指针向后移动

但是下面的代码工作正常

Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from studentinfo");
int counter=0;
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
int row = rs.getRow();
if(row==5)
{
if(counter==0)
{
counter++;
rs.absolute(4);
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
}
}
con.close();

在此代码中,如果当前行等于第 5 行,我会将指针移回到第 4 行

正如你所看到的,我没有在 createStatement() 方法中给出任何参数,即默认情况下它应该是**不可滚动**,因此上面的代码不应该获取结果,但它工作正常怎么会这样?

最佳答案

JDBC specification说:

The default ResultSet type is TYPE_FORWARD_ONLY.

The method DatabaseMetaData.supportsResultSetType returns true if the specified type is supported by the driver and false otherwise.

If the driver does not support the type supplied to the methods createStatement, prepareStatement, or prepareCall, it generates an SQLWarning on the Connection object that is creating the statement. When the statement is executed, the driver returns a ResultSet object of a type that most closely matches the requested type. An application can find out the type of a ResultSet object by calling the method ResultSet.getType.

  • 检查您的 JDBC 驱动程序是否支持 TYPE FORWARD_ONLY

  • 检查您是否收到 SQLWarning

  • 检查您获得的 ResultSet 类型。

关于java - 如果结果集不可滚动,则绝对函数不应该工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com