1

i have jtable with 2 column then i want to insert value of it to database;

i know it can be done with something like ..

    int count=table.getRowCount();
for(int i=0;i<count;i++){
Object obj1 = table.getValueAt(i, 0);
Object obj2 = table.getValueAt(i, 1);

the problem is.. getRowCount doesn't know if the row is empty, then in database, that empty value will still inserted (and it will generate my auto increment value in database)

my question how to insert database from jtable, but the empty row will not inserted?

thanks a lot for any kind of help,,

if i asking too much please just give me a clue to handle the empty row ,

forgive my english

here's the broken method to add data to database

public void addToDatabase(){
            for (int i=0;i<table.getRowCount();i++){

                //#####################################
                //maybe need something in here
                if(table.getValueAt(i, 0)==null||table.getValueAt(i, 1) ==null){
                    return;
                //and maybe need something not return, but to get the next row (Just.. maybe ...)    
                //#####################################

                }else{
                    try{
                        Connection c = getCon("databasez");
                        PreparedStatement p = c.prepareStatement("INSERT INTO table_data VALUES (?,?)");
                        p.setString(1, table.getValueAt(i, 0).toString());
                        p.setFloat (Float.parseFloat(table.getValueAt(i, 1).toString()));
                        p.executeUpdate();
                        p.close();
                    }catch(Exception e){
                        JOptionPane.showMessageDialog(this, "THERE WAS AN INVALID DATA");
                    }
                    }
                }
    }
mopr mopr
  • 193
  • 2
  • 4
  • 13

3 Answers3

1

Assuming that you're using DefaultTableModel, the result returned by getValueAt() will be null until you edit the corresponding cell.

You can also register a TableModelListener to receive a TableModelEvent that can tell your listener what has changed.

private JTable table = new JTable(1, 2);
...
table.getModel().addTableModelListener(new TableModelListener() {

    @Override
    public void tableChanged(TableModelEvent e) {
        System.out.println(""
            + e.getType() + " "
            + e.getFirstRow() + " "
            + e.getLastRow() + " " 
            + e.getColumn());
    }
});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks a lot for the response.. day by day i think about it, its will more complicated for me to use table Model Listener to know which row is have a value.. then get all row that have a value to insert into database.. its complicated for me.. but maybe the other will not feel complicated.thanks for the answer – mopr mopr Oct 26 '12 at 15:53
  • It's a little complicated but very flexible. Consider opening a _modal_ dialog to collect data for inserting a new row; listen to the `TableModel` for updates; use [checkboxes](http://stackoverflow.com/a/4528604/230513) to mark rows for deletion, etc. – trashgod Oct 26 '12 at 19:21
  • yeah.. i use jdialog to insert a new row(new data).. (truthfully this is that i want to avoid, cuz i think user will feel that my application is complex(cz my application already use many jdialog)).. but, at the end.. i use exactly what you say.. thanks dude.. – mopr mopr Oct 29 '12 at 22:17
0

maybe it will help you. http://www.exampledepot.com/egs/javax.swing.table/InsertRow.html

first, I just saw getData methods here and I didn't see insert or some alike method you invoked, so I think the code doesn't relate to what you said.

second, also you can learn to search the api about jtable. if you full know the methods of a class and how to use it in the general ways, I think you understand and use it very well. and this can also improve your understanding to understand something similar. and you will become stronger and stronger to learn other difficult things without asking questions.

That's my oppion to learn program.

Tim Li
  • 215
  • 1
  • 2
  • 8
  • sory for long time.. the electricity is down in my place.. (i need to move to the other place T-T ) sory for the code (my bad ~,~).. i already change the code and add my broken method.. – mopr mopr Oct 25 '12 at 12:23
0
JAVA swing,SIMPLE WAY TO store jTable DATA in database


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) // swing Button
{
 String name[] =new String[4]; // name is array and index 4 means no. of row 
 String age[]=new String[4]; //age is array and index 4 means no. of row  
//loop from 0 row to 4
for(int i=0;i<4;i++) 
 {
 name[i]=table.getValueAt(i,0).toString(); // it get value from 0 row and 0 column
 age[i]=table.getValueAt(i,1).toString();  // it get value from 0 row and 1 column

//similarly for more column  

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/crm","root","");
Statement s1=con.createStatement();
int mc=s1.executeUpdate("insert into testtable(Name,Age) values('"+name[i]+"','"+age[i]+"')");

}

        catch(Exception ex)
        {
            javax.swing.JOptionPane.showMessageDialog(null,ex.getMessage().toString());

        }
}
Abby
  • 1
  • 2