getValueAt()方法返回null

编程入门 行业动态 更新时间:2024-10-28 10:21:37
本文介绍了getValueAt()方法返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图将defaulttablemodel的所有值保存到我的sql数据库中,但是每当我尝试通过table.valueAt()在最后插入的行上打印该值时,它都将返回null.

I am trying to save all the values of a defaulttablemodel to my sql database but whenever I put try to print the value on the last inserted row via table.valueAt(), it returns 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!"); }

推荐答案

关于第二个问题. 您可以先在表中检查变量,然后再将其放入查询中.例如,您可以执行以下操作:

As for your second question. You can check your variable in your table before you put it inside your query. For example you can do this:

String item1; if(table.getValueAt(i, 0) != null) item1 = table.getValueAt(i, 0); else item1 = null;

用"table.getValueAt(i,0)"替换item1并为"table.getValueAt(i,2)"创建一个item2

Replace item1 with you "table.getValueAt(i, 0)" and create an item2 for your "table.getValueAt(i,2)"

String sql1 = "INSERT INTO HREmpListofCard (EmpID, CardNbr, Status, Remarks) VALUES (" +"'"+empID +"','"+ITEM1 +"','"+val +"','"+ITEM2+"')";

我还没有测试过,所以不确定它是否可以工作:)

I haven't tested this, so I'm not sure if it will work :)

祝你好运!

更多推荐

getValueAt()方法返回null

本文发布于:2023-10-31 05:23:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   getValueAt   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!