我有以下代码:
try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","VENIVIDIVICI");
Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery("SELECT * FROM example184"))
{
rs.absolute(2);
rs.absolute(1);
rs.beforeFirst();
while (rs.next())
{
System.out.println(rs.getString("iD"));
}
while(rs.previous())
{
System.out.println(rs.getString("Id"));
}
}
catch(SQLException sql ){System.out.println(sql);}
}
它应该给出一个 SQLException ,因为它是 only ResultSet.TYPE_FORWARD_ONLY
,而是它可以正常工作而不会引发任何异常。
更新:正如 Aleroot 在以下回复中建议的那样,我尝试了以下修改:
try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","VENIVIDIVICI");
Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery("SELECT * FROM example184"))
{
st.setFetchSize(Integer.MIN_VALUE);
rs.afterLast();
while (rs.previous())
{
System.out.println(rs.getString("iD"));
}
}
catch(SQLException sql ){System.out.println(sql);}
}
}
如果没有成功,它仍然可以正常编译和运行。
谢谢,确实是