当前位置: 技术问答>java相关
如何使用ResultSet对象的last()以及first()方法?何为“只转发结果集”?
来源: 互联网 发布时间:2015-08-15
本文导语: //下面的code很简单,但就是报错,我…… //环境 :MS Windows 2k Pro + J2SDK1.4 Beta3 +JCreator25 import java.util.*; import java.sql.*; public class DataSet { Connection conn; Statement stmt; ResultSet rs; int rowCo...
//下面的code很简单,但就是报错,我……
//环境 :MS Windows 2k Pro + J2SDK1.4 Beta3 +JCreator25
import java.util.*;
import java.sql.*;
public class DataSet
{
Connection conn;
Statement stmt;
ResultSet rs;
int rowCount = 0;
/** -------------Database constructor-----------
* constructor takes server providing database
*/
public DataSet()
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url=new String("jdbc:oracle:thin:@127.0.0.1:1521:ora8");
conn = DriverManager.getConnection(url,"scott","tiger");
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {e.printStackTrace();}
catch (SQLException se) { se.printStackTrace(); }
}
/** ------------------close-------------
* closes connection to database server
*/
public void close(boolean rsexit)
{
try
{
if(rsexit)
rs.close();
stmt.close();
conn.close();
}catch(SQLException closeE){closeE.printStackTrace();}
}
public void go()
{
try{
rs = stmt.executeQuery("SELECT * FROM sm_employee");
if(rs.last())//第46行!
rowCount = rs.getRow();
}catch(SQLException e){e.printStackTrace();}
System.out.print(rowCount);
}
public static void main(String[] args)
{
DataSet ds = new DataSet();
ds.go();
ds.close(true);
}
}
/*提示信息如下
java.sql.SQLException: 对只转发结果集的无效操作: last
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:210)
at oracle.jdbc.driver.BaseResultSet.last(BaseResultSet.java:90)
at DataSet.go(DataSet.java:44)
at DataSet.main(DataSet.java:55)
*/
//环境 :MS Windows 2k Pro + J2SDK1.4 Beta3 +JCreator25
import java.util.*;
import java.sql.*;
public class DataSet
{
Connection conn;
Statement stmt;
ResultSet rs;
int rowCount = 0;
/** -------------Database constructor-----------
* constructor takes server providing database
*/
public DataSet()
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url=new String("jdbc:oracle:thin:@127.0.0.1:1521:ora8");
conn = DriverManager.getConnection(url,"scott","tiger");
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {e.printStackTrace();}
catch (SQLException se) { se.printStackTrace(); }
}
/** ------------------close-------------
* closes connection to database server
*/
public void close(boolean rsexit)
{
try
{
if(rsexit)
rs.close();
stmt.close();
conn.close();
}catch(SQLException closeE){closeE.printStackTrace();}
}
public void go()
{
try{
rs = stmt.executeQuery("SELECT * FROM sm_employee");
if(rs.last())//第46行!
rowCount = rs.getRow();
}catch(SQLException e){e.printStackTrace();}
System.out.print(rowCount);
}
public static void main(String[] args)
{
DataSet ds = new DataSet();
ds.go();
ds.close(true);
}
}
/*提示信息如下
java.sql.SQLException: 对只转发结果集的无效操作: last
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:210)
at oracle.jdbc.driver.BaseResultSet.last(BaseResultSet.java:90)
at DataSet.go(DataSet.java:44)
at DataSet.main(DataSet.java:55)
*/
|
转发==forwardOnly? if you want to use last(), do not use forward only cursor
by default, with createStatement(), you will get a forwardonly readonly resultset, try
stmt = conn.createStatement(TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_READ_ONLY);
by default, with createStatement(), you will get a forwardonly readonly resultset, try
stmt = conn.createStatement(TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_READ_ONLY);
|
要设置ResultSet的可滚动性,需要指定生成Statement时的相应值:
Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY)
其中,前一个参数有三种选择:ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.TYPE_FORWARD_ONLY;
后一个有两种:ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_UPDATABLE
Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY)
其中,前一个参数有三种选择:ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.TYPE_FORWARD_ONLY;
后一个有两种:ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_UPDATABLE
|
你的stmt = conn.createStatement();结果集不用用LAST方法,必须加参数;
stmt = conn.createStatement(ResultSet.....here,some code...);
stmt = conn.createStatement(ResultSet.....here,some code...);