public void tableChanged(TableModelEvent e) {
//数据改变后的操作,相信还可以写得更好
System.out.println(e.getFirstRow() + ":" + e.getColumn());
//e.getColumn() == -1,表示插入新行,在后面可以写对新行的操作,这里就不废话了
if (e.getColumn() != -1) {
Object o;
//这两列是两个成绩,修改后更新总分和几个判断
if (e.getColumn() == 3 || e.getColumn() == 6|| e.getColumn() == 9|| e.getColumn() == 10) {
//获取某个格的数据
o = tabData.getValueAt(e.getFirstRow(), 3);
double b = 0.00;
b =Double.valueOf(o.toString());
System.out.println(b);
double a = 0.00;
//空检查很重要
if (o != null) {
a =Double.valueOf(o.toString());
}
o = tabData.getValueAt(e.getFirstRow(), 6);
double c = 0.00;
c =Double.valueOf(o.toString());
System.out.println(c);
if (o != null) {
a += Double.valueOf(o.toString());
}
o = tabData.getValueAt(e.getFirstRow(), 9);
double d = 0.00;
d =Double.valueOf(o.toString());
System.out.println(d);
if (o != null) {
a += Double.valueOf(o.toString());
}
o = tabData.getValueAt(e.getFirstRow(), 10);
if (o != null) {
a += Double.valueOf(o.toString());
}
System.out.println(a);
//设置某个格的数据
//tabData.setValueAt(b, e.getFirstRow(), 3);
//tabData.setValueAt(c, e.getFirstRow(), 6);
//tabData.setValueAt(d, e.getFirstRow(), 9);
tabData.setValueAt(a, e.getFirstRow(), 11);
//boolean b = a >= 120;
//tabData.setValueAt(b, e.getFirstRow(), 12);
}
o = tabData.getValueAt(e.getFirstRow(), 0);
if (o != null||rs!=null) {
            {
                //转换到模型行
                int select = e.getFirstRow();
                if (select == -1)
                {
                    return;
                }
                select = tabData.getRowSorter().convertRowIndexToModel(select);
                try
                {
                 rs.beforeFirst();
                    rs.absolute(select + 1);
                    //更新数据集中某个位置的内容
                    rs.updateObject(e.getColumn() + 1, tabData.getValueAt(e.getFirstRow(), e.getColumn()));
                    //将内容更新到数据库
                    rs.updateRow();                  
                    //rs.close();
                } catch (SQLException ex)
                {
                    Logger.getLogger(ConnectionSQL.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        //执行以后报诸如下面这样的错误但有时是可以运行并且不报错的。
2009-10-13 17:54:52 SQLC.ConnectionSQL tableChanged
严重: null
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Row update failed.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)