1
我想将defaulttablemodel的所有值保存到我的sql数据库,但每当我尝试通过table.valueAt()打印最后插入的行上的值时,它将返回null。getValueAt()方法返回null
try{
System.out.print(table.getValueAt(5,0)); //<- this returns null even if the table.getRowCount() is 6
for(int i=0; i<table.getRowCount();i++){
if((Boolean)table.getValueAt(i,1)) val=1;
else val=0;
//System.out.print(table.getValueAt(i, 0) +","+ val);
String sql1 = "INSERT INTO HREmpListofCard (EmpID, CardNbr, Status, Remarks) VALUES ("
+"'"+empID
+"','"+table.getValueAt(i, 0).toString()
+"','"+val
+"','"+table.getValueAt(i,2).toString()+"')";
try {
DBConnect.getConnection().createStatement().executeUpdate(sql1);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ListOfCardID.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(ListOfCardID.class.getName()).log(Level.SEVERE, null, ex);
}
}
}catch(Exception e){
System.out.print("\nerror!");
}
那么列0中的值实际上是否为空?如果不是你把什么放入桌子模型?还要注意,你不必显式地调用'toString()',在串联中你只能写'... + table.getValueAt(i,0)+ ...'。编译器会将它改为'... + String.valueOf(table.getValueAt(i,0))+ ...',它将正确处理null。 – Thomas 2012-04-22 07:07:22
row5,column0单元格显示的值是多少? – Bitmap 2012-04-22 07:08:53
感谢您的回复。我确实通过jTable在第5行第0列上添加了一个值,但是当我保存并打印该值时,它将返回null,是否必须放置类似table.fire的内容?在尝试保存值之前? – John 2012-04-22 07:24:42