ResultSet类和AbstractTableModel的一个问题, 请高手指点.

IEQQ 2004-10-26 01:08:32
//前提条件是Statement是 ResultSet.TYPE_SCROLL_SENSITIVE类型的, 支持游标滚动
//但是在 调用 rs.last(); 方法 和 rs.absolute(int x) 还是会出问题.
//各位大哥指点一二. 小弟这厢有礼了.

import javax.swing.table.*;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class myTableModel
extends AbstractTableModel
{
private Statement stmt;
private ResultSet rs = null;


public myTableModel(Statement sta, String sqlstr)
{
stmt = sta;
try
{
rs = stmt.executeQuery(sqlstr);
}
catch (SQLException ex)
{
System.out.println(ex.toString());
}
}

public void refush(String sqlstr)
{
try
{
rs = stmt.executeQuery(sqlstr);
}
catch (SQLException ex)
{
System.out.println(ex.toString());
}
}

public int getRowCount()
{
int num = 0;
if (rs == null)
return 0;
try
{
rs.last();
num = rs.getRow();
rs.beforeFirst();
return num;
}
catch (SQLException ex)
{
ex.printStackTrace();
return 0;
}
}

public int getColumnCount()
{
int count =0;
if (rs == null)
return 0;
else
{
try
{
count = rs.getMetaData().getColumnCount();
return count;
}
catch (SQLException ex)
{
ex.printStackTrace();
return 0;
}
}
}

public Object getValueAt(int rowIndex, int columnIndex)
{
if (rs == null)
return null;
else
{
try
{
rs.absolute(rowIndex +1);
return rs.getString(columnIndex +1 );
}
catch (SQLException ex)
{
return null;
}
}
}

public String getColumnName(int index)
{
if (rs == null)
return "";
else
{
try
{
String ColumName = rs.getMetaData().getColumnName(index+1);
return ColumName;
}
catch (SQLException ex)
{
return "ERROR";
}
}
}

}
...全文
197 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
IEQQ 2004-11-11
  • 打赏
  • 举报
回复
再up
IEQQ 2004-11-01
  • 打赏
  • 举报
回复
再up
IEQQ 2004-10-28
  • 打赏
  • 举报
回复
谢谢关注。问题出在下面做记号的地方。

public Object getValueAt(int rowIndex, int columnIndex)
{
if (rs == null)
return null;
else
{
try
{
//////////////////////////////////////////////////////////
//问题就出在下面这一句,是SQL的内部错误, 不能跟踪的。
//我猜测 是游标移动时出了问题, 但是我在CreateStatement的时候,指定了游标是可移动的啊.
//诸位可以把上面的代码拷到JBuilder下,用一下, 看你们用的时候会不会出问题.

rs.absolute(rowIndex +1);
///////////////////////////////////////////////////////////
return rs.getString(columnIndex +1 );
}
catch (SQLException ex)
{
return null;
}
}
}
bp69 2004-10-27
  • 打赏
  • 举报
回复
rs.last()--->Moves the cursor to the last row in this ResultSet object.返回boolean
rs.absolute(int x) ---> Moves the cursor to the given row number in this ResultSet object.返回 boolean
rs.getRow() --->Retrieves the current row number.返回int
1,你可以debug看看哪个数据出问题了
2,把errors贴出来
bp69 2004-10-27
  • 打赏
  • 举报
回复
出的问题是什么啊
IEQQ 2004-10-26
  • 打赏
  • 举报
回复
自己up

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧