我写了一个应用程序。使用JDBC和JTable
显示一些信息的J2SE。为了将数据提取到JTable
我写了一个JTableModel
,当我想要创建一个新的JTable
时,我也会创建一个新的实例到表的模型,当我打开一个包含的表单时JTable
,没有问题发生。但是当我同时打开第二张表格时,它会引发错误:
Column count is out of range.
我该如何解决?
我的jTableModel代码是这样的:
public DBGrid(Connection conn, String Query) throws SQLException
{
try
{
connection= conn;
statement= connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
Connected= true;
SetQuery(Query);
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
Connected= false;
}
}
和创建模型继承的代码是这样的:
if(FormName.equals("Form1"))
{
if(CheckOpenForm("War_Group_Frame", "Groups"))
return;
DBGrid GridModel= new DBGrid(getDATABASE_URL(), getUserName(), getPassword(),
"select * from ware where Grp_ID= 0");
War_Group_Frame W_F= new War_Group_Frame();
W_F.WarList.setModel(GridModel);
W_F.setVisible(true);
Sender.add(W_F);
BringFrameToFront(W_F);
}
else
if(FormName.equals("Form2"))
{
if(CheckOpenForm("Ware_Unit", "Units"))
return;
Ware_Unit U_F= new Ware_Unit();
U_F.UnitList.setModel(new DBGrid(connection, "select * from ware"));
U_F.setVisible(true);
Sender.add(U_F);
BringFrameToFront(U_F);
}
此jTableModel类方法出现错误:
public Class getColumnClass(int column) throws IllegalStateException
{
if(!Connected)
throw new IllegalStateException(java.util.ResourceBundle.getBundle("God_Lover/Resources").getString("DBNotConnectedError"));
try
{
if(column< getColumnCount())
{
String ClassName = metaData.getColumnClassName(column + 1);
return Class.forName(ClassName);
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
return Object.class;
}