社区
Java EE
帖子详情
JDBC的“ResultSet can not re-read row data for column 1”问题有解决办法吗?
MINJI
2008-09-17 12:47:09
看文档是微软的JDBC有BUG,我下载了sql2005的jdbc来用,结果还是一样。除了按字段顺序读外,还有什么办法吗?有什么其他的免费JDBC代替一下吗?谢谢!
...全文
45
2
打赏
收藏
JDBC的“ResultSet can not re-read row data for column 1”问题有解决办法吗?
看文档是微软的JDBC有BUG,我下载了sql2005的jdbc来用,结果还是一样。除了按字段顺序读外,还有什么办法吗?有什么其他的免费JDBC代替一下吗?谢谢!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
zhongwenly
2008-09-26
打赏
举报
回复
jdbc不允许回读操作,就是说你要按照字段的先后顺序读取字段
yeah920
2008-09-26
打赏
举报
回复
帮你顶一下.
sqlserver2000re_
read
错误的解决
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
JDBC
]
ResultSet
can not re-
read
row
data
for
column
5的解决方案
数据库工具类
Data
baseUtil.java
package com.hexiang.utils; import java.sql.*; import java.util.*; /** * * Title: 数据库工具类 * * * Description: 将大部分的数据库操作放入这个类中, 包括数据库连接的建立, 自动释放等. * * * @author beansoft 日期: 2004年04月 * @version 2.0 */ public class
Data
baseUtil { /** 数据库连接 */ private java.sql.Connection connection; /** * All
data
base resources created by this class, should be free after all * operations, holds:
ResultSet
, Statement, PreparedStatement, etc. */ private ArrayList resourcesList = new ArrayList(5); public
Data
baseUtil() { } /** 关闭数据库连接并释放所有数据库资源 */ 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++) { close
JDBC
Resource(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 close
JDBC
Resource(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
Data
baseUtil.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
data
base connection and return it. * * @return Statement */ public Statement getStatement() { // 首先尝试获取可滚动的 Statement, 然后才是普通 Statement Statement up
data
bleStmt = getUp
data
bleStatement(); if (up
data
bleStmt != null) return up
data
bleStmt; 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 up
data
ble and scrollable statement from the
data
base connection * and return it. * * @return Statement */ public Statement getUp
data
bleStatement() { try { Statement statement = getConnection() .createStatement(
ResultSet
.TYPE_SCROLL_SENSITIVE,
ResultSet
.CONCUR_UP
DATA
BLE); this.getResourcesList().add(statement); return statement; } catch (Exception ex) { System.out.println("Error in getUp
data
bleStatement(): " + ex); } return null; } /** * Create a prepared statement and return it. * * @param sql * String SQL to prepare * @th
row
s SQLException * any
data
base exception * @return PreparedStatement the prepared statement */ public PreparedStatement getPreparedStatement(String sql) th
row
s SQLException { try { PreparedStatement preparedStatement = getConnection() .prepareStatement(sql,
ResultSet
.TYPE_SCROLL_INSENSITIVE,
ResultSet
.CONCUR_UP
DATA
BLE); 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
column
Name * the
column
name * @return the fetched string */ public static String getString(
ResultSet
rs, String
column
Name) { try { String result = rs.getString(
column
Name); if (result == null) { result = ""; } return result; } catch (Exception ex) { } return ""; } /** * Get all the
column
labels * * @param
resultSet
*
ResultSet
* @return String[] */ public static String[] get
Column
s(
ResultSet
resultSet
) { if (
resultSet
== null) { return null; } try {
ResultSet
Meta
Data
meta
Data
=
resultSet
.getMeta
Data
(); int numberOf
Column
s = meta
Data
.get
Column
Count(); if (numberOf
Column
s <= 0) { return null; } String[]
column
s = new String[numberOf
Column
s]; //System.err.println("numberOf
Column
s=" + numberOf
Column
s); // Get the
column
names for (int
column
= 0;
column
< numberOf
Column
s;
column
++) { // System.out.print(meta
Data
.get
Column
Label(
column
+ 1) + "\t");
column
s[
column
] = meta
Data
.get
Column
Name(
column
+ 1); } return
column
s; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * Get the
row
count of the result set. * * @param
resultset
*
ResultSet
* @th
row
s SQLException * if a
data
base access error occurs or the result set type is * TYPE_FORWARD_ONLY * @return int the
row
count * @since 1.2 */ public static int get
Row
Count(
ResultSet
resultset
) th
row
s SQLException { int
row
= 0; try { int current
Row
=
resultset
.get
Row
(); // Remember old
row
position
resultset
.last();
row
=
resultset
.get
Row
(); if (current
Row
> 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 get
Column
Count(
ResultSet
resultSet
) { if (
resultSet
== null) { return 0; } try {
ResultSet
Meta
Data
meta
Data
=
resultSet
.getMeta
Data
(); int numberOf
Column
s = meta
Data
.get
Column
Count(); return numberOf
Column
s; } 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 "
column
Name" = "value", where value is converted to * String. * * @param
resultSet
*
ResultSet
* @return Hashtable */ public static final Hashtable
read
ResultToHashtable(
ResultSet
resultSet
) { if (
resultSet
== null) { return null; } Hashtable resultHash = new Hashtable(); try { String[]
column
s = get
Column
s(
resultSet
); if (
column
s != null) { //
Read
data
column
by
column
for (int i = 0; i <
column
s.length; i++) { resultHash.put(
column
s[i], getString(
resultSet
,
column
s[i])); } } } catch (Exception ex) { ex.printStackTrace(); } return resultHash; } /** *
Read
data
from result set automatically and put the result it a * hashtable. Stored as "
column
Name" = "value", where value is converted to * String. * * Note: assume the default
data
base string encoding is ISO8859-1. * * @param
resultSet
*
ResultSet
* @return Hashtable */ @SuppressWarnings("unchecked") public static final Hashtable
read
ResultToHashtableISO(
ResultSet
resultSet
) { if (
resultSet
== null) { return null; } Hashtable resultHash = new Hashtable(); try { String[]
column
s = get
Column
s(
resultSet
); if (
column
s != null) { //
Read
data
column
by
column
for (int i = 0; i <
column
s.length; i++) { String isoString = getString(
resultSet
,
column
s[i]); try { resultHash.put(
column
s[i], new String(isoString .getBytes("ISO8859-1"), "GBK")); } catch (Exception ex) { resultHash.put(
column
s[i], isoString); } } } } catch (Exception ex) { ex.printStackTrace(); } return resultHash; } /** Test this class. */ public static void main(String[] args) th
row
s Exception {
Data
baseUtil util = new
Data
baseUtil(); // TODO: 从连接池工厂获取连接 // util.setConnection(ConnectionFactory.getConnection());
ResultSet
rs = util.executeQuery("SELECT * FROM e_hyx_trans_info"); while (rs.next()) { Hashtable hash =
read
ResultToHashtableISO(rs); Enumeration keys = hash.keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); System.out.println(key + "=" + hash.get(key)); } } rs.close(); util.close(); } }
Excel POI读取封装(文件+示范代码)
Excel POI读取封装(文件+示范代码) package org.excel.service; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.
ResultSet
; import java.sql.SQLException; import java.sql.Statement; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import javax.jws.WebService; import org.apache.poi.hssf.usermodel.*; import org.excel.
data
.
Data
Type; import org.excel.
data
.DealForeign; import org.excel.
data
.ExcelImport; import org.excel.
data
.impl.StInStatusImpl; import org.excel.
data
.impl.StuClassImportImpl; import org.excel.
data
.impl.StuEducationImpl; import org.excel.
data
.impl.StuImprot
Data
Impl; import org.excel.
data
.impl.StuInClassImportImpl; import org.excel.
data
.impl.StuWorkStatusImpl; import org.excel.manager.Util; import org.excel.model.ExcelMap; import org.excel.xml.ExcelXmlParse; import net.sourceforge.jtds.
jdbc
x.Jtds
Data
Source; @WebService public class ExcelServiceImpl implements IExcelService { String tableName = ""; public static Integer suc = 0; public static Integer fail = 0; StringBuilder insert = new StringBuilder( " insert into {tableName} {
column
} values {values}"); /** 读取Excel表格数据 */ public List
read
Excel(String excelName, Integer sheetIndex, String startPoint, String endPoint) th
row
s Exception { FileInputStream inputStream = null; inputStream = new FileInputStream(excelName); HSSFWorkbook workbook = new HSSFWorkbook(inputStream); HSSFSheet sheet = workbook.getSheetAt(sheetIndex); String[] sc = null;// 开始坐标 String[] ec = null;// 结束坐标 int start
Row
= 0;// 默认开始行数 int endRoe = sheet.getPhysicalNumberOf
Row
s();// 默认结束行 int startLine = 0;// 默认开始列 int endLine = 0;// 结束列 if (startPoint != null && endPoint != null) { sc = startPoint.
关于
ResultSet
can not re-
read
row
data
for
column
1 解决方法
今天早上弄完了网站,就去弄Java了,居然遇到了
问题
“
ResultSet
can not re-
read
row
data
for
column
1”,上网搜了下,原来是微软公司的驱动的兼容性不太好. 热心人总结了微软驱动的缺点: (1)如果采用
jdbc
-odbc驱动,那么就必须按照查询顺序来一次读取(不论有没有image或text类型) (2)如果采用微软提供的ms sql serve...
SQLServer2000 驱动错
ResultSet
can not re-
read
row
data
for
column
ResultSet
can not re-
read
row
data
for
column
已有 260 次阅读 2010-05-07 10:49
今天遇到了
问题
“
ResultSet
can not re-
read
row
data
for
column
4”,上网搜了下,原来是微软公司的驱动的兼容性不太好。下面引自csdn博客作者xwhope的文章。
有热心人总结了微软驱动的缺点:
(1)如果采用
jdbc
-odbc驱动,那么就必须按照查询顺序来一次读取(不论有没
Java EE
67,513
社区成员
225,879
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章