2

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!");
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
John
  • 836
  • 2
  • 25
  • 39
  • So is the value in column 0 actually null? If not what did you put into the table model? Also note that you don't have to call `toString()` explicitly, in concatenation you could only write ` ... + table.getValueAt(i, 0) + ...`. The compiler will change that to ` ... + String.valueOf( table.getValueAt(i, 0) ) + ... ` which will handle null correctly. – Thomas Apr 22 '12 at 07:07
  • What is the value shown at cell of row5,column0? – Bitmap Apr 22 '12 at 07:08
  • thanks for the reply. i did put a value on row 5 column 0 via the jTable, but when i save it and print the value, it returns null, do i have to put something like table.fire.. before i try to save the values? – John Apr 22 '12 at 07:24
  • Notice that you have to escape all the statement parameters *on your own*. Otherwise, depending on where the input comes from, this code allows SQL injection. Just FYI ;) – spidey Apr 22 '12 at 07:27
  • yup spidey! thanks for reminding me. I'll do that later. I just need to figure this out first. – John Apr 22 '12 at 07:30
  • That's the spirit! Sorry for telling you then. Whenever I see such code I'm like "omg, I just hope he cares for security, please...". – spidey Apr 22 '12 at 07:32
  • Im not sure what happened but after i remove the .toString() mentioned by thomas, it suddenly work! the only problem now is that it store the string 'null' into the database instead of just a null value. do you guys have any idea how to fix that? – John Apr 22 '12 at 07:51
  • Update your question to include an [sscce](http://sscce.org/) that shows what you've got working. – trashgod Apr 22 '12 at 09:21

1 Answers1

2

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;

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 :)

Good luck!

Byron Voorbach
  • 4,365
  • 5
  • 27
  • 35