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";
}
}
}
}