我在程序中实现AbstractTableModel中的setValueAt方法,将对table中的修改提交到数据库中出现了如下错误:
java.lang.ClassCastException: java.lang.Integer
at MyTableModel.setValueAt(MyTableModel.java:82)
方法如下:
public void setValueAt(Object value,int row,int col){
Integer newValue;
Vector currentRow=(Vector)rowData.get(row);
try{
newValue=Integer.valueOf((String)value); //这是第82行
}
catch(NumberFormatException e1){
newValue=new Integer(0);
}
String id=(String)currentRow.get(0);
String columnName=col==2?"MIDEXAM":"FINALEXAM";
try{
stmt.executeUpdate("update student set "+columnName+"="+newValue+" where ID='"+id+"'");
currentRow.set(col,newValue);
float average=(((Integer)currentRow.get(2)).intValue()+((Integer)currentRow.get(3)).intValue())/(float)2.0;
currentRow.set(4,new Float(average));
fireTableCellUpdated(row,4);
}
catch(SQLException e2){
System.out.println(e2);
}
}
java.lang.ClassCastException: java.lang.Integer
at MyTableModel.setValueAt(MyTableModel.java:82)
方法如下:
public void setValueAt(Object value,int row,int col){
Integer newValue;
Vector currentRow=(Vector)rowData.get(row);
try{
newValue=Integer.valueOf((String)value); //这是第82行
}
catch(NumberFormatException e1){
newValue=new Integer(0);
}
String id=(String)currentRow.get(0);
String columnName=col==2?"MIDEXAM":"FINALEXAM";
try{
stmt.executeUpdate("update student set "+columnName+"="+newValue+" where ID='"+id+"'");
currentRow.set(col,newValue);
float average=(((Integer)currentRow.get(2)).intValue()+((Integer)currentRow.get(3)).intValue())/(float)2.0;
currentRow.set(4,new Float(average));
fireTableCellUpdated(row,4);
}
catch(SQLException e2){
System.out.println(e2);
}
}
解决方案 »
- SCJP中的一道泛型题
- 关于JAVA中String 编码的问题
- 如何实现一天之内对用户登录次数作限制?
- 求助 !!!
- Swing中至少运行两个线程…………关于事件分发线程Event-dispatching thred
- 新手问题,谢谢大家的回答!!!
- Java确实有好多匪夷所思的东西,下面又是一个奇怪的疑问。
- RepaintManager是个什么东东
- String相加的问题
- 怎样才能让Java写的程序在一般的Windows平台(没有装JDK的机器)上运行
- socket 多线程 (3000条以上时出现java.net.ConnectException: Connection timed out: connect
- 这个说法对吗??
改为
newValue=Integer.valueOf(value.toString()); //这是第82行
或
newValue=(Integer)value; //这是第82行
因为Integer不是String的子类