社区
Java SE
帖子详情
我怎么用不了ResultSet.absolute()方法?
GFox
2001-09-02 09:32:00
我用的是 weblogic 的 mssqlserver4v70 驱动,它用不了,好像是说没有实现?
难道说 weblogic 的不是 jdbc 2.0 的?要是我想用这个方法应该怎么办?
其实我主要是想移动游标,有没有其它的方法?
...全文
87
4
打赏
收藏
我怎么用不了ResultSet.absolute()方法?
我用的是 weblogic 的 mssqlserver4v70 驱动,它用不了,好像是说没有实现? 难道说 weblogic 的不是 jdbc 2.0 的?要是我想用这个方法应该怎么办? 其实我主要是想移动游标,有没有其它的方法?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
chenyun2000
2001-09-03
打赏
举报
回复
不是你的WEBLOGIC不支持JDBC2。0,是驱动程序不支持。WEBLOGIC 6 应该支持JDBC2。0的。你可以使用java.sql.DatabaseMeta类的相应方法检测该驱动程序是否支持光标的不敏感、敏感类型
tiangou
2001-09-03
打赏
举报
回复
用jdk1.3
riverhorse
2001-09-02
打赏
举报
回复
有的driver不是全部支持,我碰到了不少类似的问题。
如:oracle的oci driver不支持store procedure 返回recordset.
oracle的thin 驱动程序中文必须进行编码。
jdbc-odbc bridge driver 只支持按列顺序取数据。
jrun 的 sql server 不支持store procedure 返回数据。
............
xxmm
2001-09-02
打赏
举报
回复
用英文说。
JDBC详解HTML-JDBC.pp
1、JDBC(Java Database Connection):java连接数据库统一接口API,底层主要通过直接的JDBC驱动和 JDBC-ODBC桥驱动实现与数据库的连接。 1>.JDBC驱动程序类型: <1>.JDBC-ODBC桥加ODBC驱动程序:需要ODBC驱动,适合于企业网或三层结构应用程序 <2>.本地API:需要驱动程序的二进制代码支持 <3>.JDBC网络纯java驱动程序:将JDBC转换为与DBMS无关的网络协议,又被某服务器转换为一种DBMS 协议,以操作各种数据库 <4>.本地协议纯java驱动程序:将JDBC调用直接转换成JDBC所使用的网络协议 2、JDBC操作基本流程: 1>.导入驱动:实例化时自动向DriverManager注册(DriverManager.registerDriver()) <1>.Class.forName(driver) <2>.Class.forName(driver).newInstance() <3>.new driver() 2>.取得数据库连接(Connect to the DataBase) <1>.用DriverManager取数据库连接 Connection cn = DriverManager.getConnection(url,uid,pwd); <2>.用jndi(java的命名和目录服务)方式:多用于jsp Context ctx = (Context) new InitialContext().lookup("java:comp/env"); DataSource ds = (DataSource) ctx.lookup(jndi); Connection cn = ds.getConnection(); 3>.执行sql语句(Execute the SQL) <1>.用Statement来执行sql语句 Statement sm = cn.createStatement(); sm.executeQuery(sql); // 执行数据查询语句(select) sm.executeUpdate(sql); // 执行数据更新语句(delete、update、insert、drop等) <2>.用PreparedStatement来执行sql语句 String sql = "insert into user (id,name) values (?,?)"; PreparedStatement ps = cn.prepareStatement(sql); ps.setInt(1,xxx); ps.setString(2,xxx); ...
ResultSet
rs = ps.executeQuery(); // 查询 int c = ps.executeUpdate(); // 更新 4>.处理执行结果: <1>.查询语句,返回记录集
ResultSet
<2>.更新语句,返回数字,表示该更新影响的记录数 <3>.
ResultSet
的
方法
:while(re.next()) next(),将游标往后移动一行,如果成功返回true;否则返回false getInt("id")或getSting("name"),返回当前游标下某个字段的值 5>.释放数据库连接 rs.close(); ps.close(); /stat.close(); con.close(); 3、创建可滚动、更新的记录集 1>.创建Statement时指定参数:该Statement取得的
ResultSet
就是可滚动的 Statement sm = cn.createStatement(
ResultSet
.TYPE_SCROLL_ENSITIVE,
ResultSet
.CONCUR_READ_ONLY); 2>.创建PreparedStatement时指定参数 PreparedStatemet ps = cn.prepareStatement(sql,
ResultSet
.TYPE_SCROLL_INSENSITIVE,
ResultSet
.CONCUR_READ_ONLY); rs.
absolute
(9000);
数据库工具类DatabaseUtil.java
package com.hexiang.utils; import java.sql.*; import java.util.*; /** * * Title: 数据库工具类 * * * Description: 将大部分的数据库操作放入这个类中, 包括数据库连接的建立, 自动释放等. * * * @author beansoft 日期: 2004年04月 * @version 2.0 */ public class DatabaseUtil { /** 数据库连接 */ private java.sql.Connection connection; /** * All database resources created by this class, should be free after all * operations, holds:
ResultSet
, Statement, PreparedStatement, etc. */ private ArrayList resourcesList = new ArrayList(5); public DatabaseUtil() { } /** 关闭数据库连接并释放所有数据库资源 */ public void close() { closeAllResources(); close(getConnection()); } /** * Close given connection. * * @param connection * Connection */ public static void close(Connection connection) { try { connection.close(); } catch (Exception ex) { System.err.println("Exception when close a connection: " + ex.getMessage()); } } /** * Close all resources created by this class. */ public void closeAllResources() { for (int i = 0; i < this.getResourcesList().size(); i++) { closeJDBCResource(getResourcesList().get(i)); } } /** * Close a jdbc resource, such as
ResultSet
, Statement, Connection.... All * these objects must have a method signature is void close(). * * @param resource - * jdbc resouce to close */ public void closeJDBCResource(Object resource) { try { Class clazz = resource.getClass(); java.lang.reflect.Method method = clazz.getMethod("close", null); method.invoke(resource, null); } catch (Exception e) { // e.printStackTrace(); } } /** * 执行 SELECT 等 SQL 语句并返回结果集. * * @param sql * 需要发送到数据库 SQL 语句 * @return a
ResultSet
object that contains the data produced * by the given query; never null */ public
ResultSet
executeQuery(String sql) { try { Statement statement = getStatement();
ResultSet
rs = statement.executeQuery(sql); this.getResourcesList().add(rs); this.getResourcesList().add(statement);// BUG fix at 2006-04-29 by BeanSoft, added this to res list // MySql 数据库要求必需关闭 statement 对象, 否则释放不掉资源 // - 此观点错误, 因为关闭此对象后有时数据无法读出 //statement.close(); return rs; } catch (Exception ex) { System.out.println("Error in executeQuery(\"" + sql + "\"):" + ex); // ex.printStackTrace(); return null; } } /** * Executes the given SQL statement, which may be an INSERT, * UPDATE, or DELETE statement or an SQL * statement that returns nothing, such as an SQL DDL statement. 执行给定的 SQL * 语句, 这些语句可能是 INSERT, UPDATE 或者 DELETE 语句, 或者是一个不返回任何东西的 SQL 语句, 例如一个 SQL * DDL 语句. * * @param sql * an SQL INSERT,UPDATE or * DELETE statement or an SQL statement that * returns nothing * @return either the row count for INSERT, * UPDATE or DELETE statements, or * 0 for SQL statements that return nothing */ public int executeUpdate(String sql) { try { Statement statement = getStatement(); return statement.executeUpdate(sql); // MySql 数据库要求必需关闭 statement 对象, 否则释放不掉资源 // - 此观点错误, 因为关闭此对象后有时数据无法读出 //statement.close(); } catch (Exception ex) { System.out.println("Error in executeUpdate(): " + sql + " " + ex); //System.out.println("executeUpdate:" + sql); ex.printStackTrace(); } return -1; } /** * 返回记录总数, 使用
方法
: getAllCount("SELECT count(ID) from tableName") 2004-06-09 * 可滚动的 Statement 不能执行 SELECT MAX(ID) 之类的查询语句(SQLServer 2000) * * @param sql * 需要执行的 SQL * @return 记录总数 */ public int getAllCount(String sql) { try { Statement statement = getConnection().createStatement(); this.getResourcesList().add(statement);
ResultSet
rs = statement.executeQuery(sql); rs.next(); int cnt = rs.getInt(1); rs.close(); try { statement.close(); this.getResourcesList().remove(statement); } catch (Exception ex) { ex.printStackTrace(); } return cnt; } catch (Exception ex) { System.out.println("Exception in DatabaseUtil.getAllCount(" + sql + "):" + ex); ex.printStackTrace(); return 0; } } /** * 返回当前数据库连接. */ public java.sql.Connection getConnection() { return connection; } /** * 连接新的数据库对象到这个工具类, 首先尝试关闭老连接. */ public void setConnection(java.sql.Connection connection) { if (this.connection != null) { try { getConnection().close(); } catch (Exception ex) { } } this.connection = connection; } /** * Create a common statement from the database connection and return it. * * @return Statement */ public Statement getStatement() { // 首先尝试获取可滚动的 Statement, 然后才是普通 Statement Statement updatableStmt = getUpdatableStatement(); if (updatableStmt != null) return updatableStmt; try { Statement statement = getConnection().createStatement(); this.getResourcesList().add(statement); return statement; } catch (Exception ex) { System.out.println("Error in getStatement(): " + ex); } return null; } /** * Create a updatable and scrollable statement from the database connection * and return it. * * @return Statement */ public Statement getUpdatableStatement() { try { Statement statement = getConnection() .createStatement(
ResultSet
.TYPE_SCROLL_SENSITIVE,
ResultSet
.CONCUR_UPDATABLE); this.getResourcesList().add(statement); return statement; } catch (Exception ex) { System.out.println("Error in getUpdatableStatement(): " + ex); } return null; } /** * Create a prepared statement and return it. * * @param sql * String SQL to prepare * @throws SQLException * any database exception * @return PreparedStatement the prepared statement */ public PreparedStatement getPreparedStatement(String sql) throws SQLException { try { PreparedStatement preparedStatement = getConnection() .prepareStatement(sql,
ResultSet
.TYPE_SCROLL_INSENSITIVE,
ResultSet
.CONCUR_UPDATABLE); this.getResourcesList().add(preparedStatement); return preparedStatement; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * Return the resources list of this class. * * @return ArrayList the resources list */ public ArrayList getResourcesList() { return resourcesList; } /** * Fetch a string from the result set, and avoid return a null string. * * @param rs * the
ResultSet
* @param columnName * the column name * @return the fetched string */ public static String getString(
ResultSet
rs, String columnName) { try { String result = rs.getString(columnName); if (result == null) { result = ""; } return result; } catch (Exception ex) { } return ""; } /** * Get all the column labels * * @param
resultSet
*
ResultSet
* @return String[] */ public static String[] getColumns(
ResultSet
resultSet
) { if (
resultSet
== null) { return null; } try {
ResultSet
MetaData metaData =
resultSet
.getMetaData(); int numberOfColumns = metaData.getColumnCount(); if (numberOfColumns <= 0) { return null; } String[] columns = new String[numberOfColumns]; //System.err.println("numberOfColumns=" + numberOfColumns); // Get the column names for (int column = 0; column < numberOfColumns; column++) { // System.out.print(metaData.getColumnLabel(column + 1) + "\t"); columns[column] = metaData.getColumnName(column + 1); } return columns; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * Get the row count of the result set. * * @param
resultset
*
ResultSet
* @throws SQLException * if a database access error occurs or the result set type is * TYPE_FORWARD_ONLY * @return int the row count * @since 1.2 */ public static int getRowCount(
ResultSet
resultset
) throws SQLException { int row = 0; try { int currentRow =
resultset
.getRow(); // Remember old row position
resultset
.last(); row =
resultset
.getRow(); if (currentRow > 0) {
resultset
.
absolute
(row); } } catch (Exception ex) { ex.printStackTrace(); } return row; } /** * Get the column count of the result set. * * @param
resultSet
*
ResultSet
* @return int the column count */ public static int getColumnCount(
ResultSet
resultSet
) { if (
resultSet
== null) { return 0; } try {
ResultSet
MetaData metaData =
resultSet
.getMetaData(); int numberOfColumns = metaData.getColumnCount(); return numberOfColumns; } catch (Exception ex) { ex.printStackTrace(); } return 0; } /** * Read one row's data from result set automatically and put the result it a * hashtable. Stored as "columnName" = "value", where value is converted to * String. * * @param
resultSet
*
ResultSet
* @return Hashtable */ public static final Hashtable readResultToHashtable(
ResultSet
resultSet
) { if (
resultSet
== null) { return null; } Hashtable resultHash = new Hashtable(); try { String[] columns = getColumns(
resultSet
); if (columns != null) { // Read data column by column for (int i = 0; i < columns.length; i++) { resultHash.put(columns[i], getString(
resultSet
, columns[i])); } } } catch (Exception ex) { ex.printStackTrace(); } return resultHash; } /** * Read data from result set automatically and put the result it a * hashtable. Stored as "columnName" = "value", where value is converted to * String. * * Note: assume the default database string encoding is ISO8859-1. * * @param
resultSet
*
ResultSet
* @return Hashtable */ @SuppressWarnings("unchecked") public static final Hashtable readResultToHashtableISO(
ResultSet
resultSet
) { if (
resultSet
== null) { return null; } Hashtable resultHash = new Hashtable(); try { String[] columns = getColumns(
resultSet
); if (columns != null) { // Read data column by column for (int i = 0; i < columns.length; i++) { String isoString = getString(
resultSet
, columns[i]); try { resultHash.put(columns[i], new String(isoString .getBytes("ISO8859-1"), "GBK")); } catch (Exception ex) { resultHash.put(columns[i], isoString); } } } } catch (Exception ex) { ex.printStackTrace(); } return resultHash; } /** Test this class. */ public static void main(String[] args) throws Exception { DatabaseUtil util = new DatabaseUtil(); // TODO: 从连接池工厂获取连接 // util.setConnection(ConnectionFactory.getConnection());
ResultSet
rs = util.executeQuery("SELECT * FROM e_hyx_trans_info"); while (rs.next()) { Hashtable hash = readResultToHashtableISO(rs); Enumeration keys = hash.keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); System.out.println(key + "=" + hash.get(key)); } } rs.close(); util.close(); } }
JSP分页技术实现.mht
目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页数据显示。这种
方法
有两个主要的缺点:一是用户可能看到的是过期数据;二是如果数据量非常大时第一次查询遍历结果集会耗费很长时间,并且缓存的数据也会占用大量内存,效率明显下降。 其它常见的
方法
还有每次翻页都查询一次数据库,从
ResultSet
中只取出一页数据(使用rs.last();rs.getRow()获得总计录条数,使用rs.
absolute
()定位到本页起始记录)。这种方式在某些数据库(如oracle)的JDBC实现中差不多也是需要遍历所有记录,实验证明在记录数很大时速度非常慢。 至于缓存结果集
ResultSet
的
方法
则完全是一种错误的做法。因为
ResultSet
在Statement或Connection关闭时也会被关闭,如果要使
ResultSet
有效势必长时间占用数据库连接。
JSP分页(技术)代码
目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页数据显示。这种
方法
有两个主要的缺点:一是用户可能看到的是过期数据;二是如果数据量非常大时第一次查询遍历结果集会耗费很长时间,并且缓存的数据也会占用大量内存,效率明显下降。 其它常见的
方法
还有每次翻页都查询一次数据库,从
ResultSet
中只取出一页数据(使用rs.last();rs.getRow()获得总计录条数,使用rs.
absolute
()定位到本页起始记录)。这种方式在某些数据库(如oracle)的JDBC实现中差不多也是需要遍历所有记录,实验证明在记录数很大时速度非常慢。 至于缓存结果集
ResultSet
的
方法
则完全是一种错误的做法。因为
ResultSet
在Statement或Connection关闭时也会被关闭,如果要使
ResultSet
有效势必长时间占用数据库连接。
很好的一个jsp分页
很好的jsp分页,目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页数据显示。这种
方法
有两个主要的缺点:一是用户可能看到的是过期数据;二是如果数据量非常大时第一次查询遍历结果集会耗费很长时间,并且缓存的数据也会占用大量内存,效率明显下降。 其它常见的
方法
还有每次翻页都查询一次数据库,从
ResultSet
中只取出一页数据(使用rs.last();rs.getRow()获得总计录条数,使用rs.
absolute
()定位到本页起始记录)。这种方式在某些数据库(如oracle)的JDBC实现中差不多也是需要遍历所有记录,实验证明在记录数很大时速度非常慢。 至于缓存结果集
ResultSet
的
方法
则完全是一种错误的做法。因为
ResultSet
在Statement或Connection关闭时也会被关闭,如果要使
ResultSet
有效势必长时间占用数据库连接。 因此比较好的分页做法应该是每次翻页的时候只从数据库里检索页面大小的块区的数据。这样虽然每次翻页都需要查询数据库,但查询出的记录数很少,网络传输数据量不大,如果使用连接池更可以略过最耗时的建立数据库连接过程。而在数据库端有各种成熟的优化技术用于提高查询速度,比在应用服务器层做缓存有效多了。
Java SE
62,614
社区成员
307,319
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章