Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getValueAt()方法返回null_Java_Sql_Swing_Jtable_Defaulttablemodel - Fatal编程技术网

Java getValueAt()方法返回null

Java getValueAt()方法返回null,java,sql,swing,jtable,defaulttablemodel,Java,Sql,Swing,Jtable,Defaulttablemodel,我试图将defaulttablemodel的所有值保存到我的sql数据库中,但每当我通过table.valueAt()在最后插入的行上输入值时,它都返回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++){

我试图将defaulttablemodel的所有值保存到我的sql数据库中,但每当我通过table.valueAt()在最后插入的行上输入值时,它都返回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!");
}
试试看{

System.out.print(table.getValueAt(5,0));//至于第二个问题。 在将变量放入查询之前,可以检查表中的变量。例如,可以执行以下操作:

 String item1;

 if(table.getValueAt(i, 0) != null)
     item1 = table.getValueAt(i, 0);
 else
     item1 = null;
将item1替换为“table.getValueAt(i,0)”,并为“table.getValueAt(i,2)”创建item2

我还没有测试过这个,所以我不确定它是否有效:)


祝你好运!至于你的第二个问题。 在将变量放入查询之前,可以检查表中的变量。例如,可以执行以下操作:

 String item1;

 if(table.getValueAt(i, 0) != null)
     item1 = table.getValueAt(i, 0);
 else
     item1 = null;
将item1替换为“table.getValueAt(i,0)”,并为“table.getValueAt(i,2)”创建item2

我还没有测试过这个,所以我不确定它是否有效:)


祝你好运!

那么列0中的值实际上是空的吗?如果不是的话,你在表模型中放了什么?还要注意,你不必显式地调用
toString()
,在串联中,你只能写“…+表.getValueAt(i,0)+…<代码>。编译器会将其更改为
…+字符串.valueOf(table.getValueAt(i,0))+…`这将正确处理null。第5行第0列的单元格中显示的值是多少?感谢您的回答。我确实通过jTable在第5行第0列中输入了一个值,但是当我保存并打印该值时,它返回null,我是否必须输入类似于table.fire的内容。在我尝试保存值之前,请注意,您必须转义所有语句pa参数。否则,根据输入来自何处,此代码允许SQL注入。仅供参考;)是的,谢谢提醒我。我稍后会这样做。我只需要先弄清楚这一点。那么列0中的值是否实际为空?如果不是,您在表模型中输入了什么?还请注意,您不必调用
toString()
显式地,在串联中,您只能写入“…+table.getValueAt(i,0)+…
。编译器将其更改为“
…+String.valueOf(table.getValueAt(i,0))+…`这将正确处理null。第5行第0列的单元格中显示的值是多少?感谢您的回答。我确实通过jTable在第5行第0列中输入了一个值,但是当我保存并打印该值时,它返回null,我是否必须输入类似于table.fire的内容。在我尝试保存值之前,请注意,您必须转义所有语句pa你自己的参数。否则,根据输入的来源,这段代码允许SQL注入。仅供参考;)是的,谢谢你提醒我。我会稍后再做。我只需要先弄清楚这一点。