0

I want to add db(MySql) data to a single specific column in jTable when there is three columns. From this below coding the data automatically add to the 1st column, but I want to add it to the 2nd column in Jtable. Please help me ..i'm new to netbeans !!!

Connection con = Driver.connect();
ResultSet rst = Handler.getData(con, "select lec_name from lecturer"); 
DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
while (rst.next()) {
   Object ob []= {rst.getString(1)};
   dtm.addRow(ob); 
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
ramindusn
  • 53
  • 5

1 Answers1

1

Each element in the Object array is a column. This means, you simply means you just need to fill the row array with the correct values

Object ob []= {rst.getString(1), rst.getString(2), rst.getString(3)}};
dtm.addRow(ob); 

This of course assumes you've added the appropriate columns to the model in the first place

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @ramindusn [everything is in Oracle tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#simple), but search for ResultsetTableModel or TableFromDatabase – mKorbel Mar 18 '13 at 07:27
  • yeah ..I agree with you ..but I want is, in JTable I have three columns and I want this single column retrieve data to be add into the 2nd column in Jtable – ramindusn Mar 18 '13 at 07:42
  • 1
    again everything is in Oracle tutorial, search here for setValueAt – mKorbel Mar 18 '13 at 07:49
  • If you just want the result to go onto the second column, you could use Object ob []= {null, rst.getString(1), null}}; to add a new row, or dim.setValueAt(row, 1, rst.getString(1)); to change the value of an existing row – MadProgrammer Mar 18 '13 at 08:00