Link to home
Start Free TrialLog in
Avatar of ram817
ram817

asked on

update database based on edited JTable

Help! I would like to be able to edit my JTable and update the database based on the changes made in the JTable. I know that I need an update sql statement but how do I know that what I'm passing to the database are actually the changes made in the JTable? I am quite lost and don't know where to start. I'd appreciate all the help! Here is my code (another class calls on diseaseEditPanel):

class diseaseEditPanel extends JPanel implements ActionListener
{
      Border bname;

      JTable diseaseTable;
      JScrollPane diseaseScroll;
      
      Vector columnNames, rows;
      
      String ewan = null;

      public diseaseEditPanel()
      {
            bname = BorderFactory.createEtchedBorder();
            setBorder(bname);
            setLayout(null);
            
      
            // Initialize a JTable with the rows of objects and column headings
            diseaseTable = new JTable(rows, columnNames);
            diseaseTable.getTableHeader().setReorderingAllowed(false);
            
            // Add the table to a JScrollPane to make it display nicely
            diseaseScroll = new JScrollPane(diseaseTable);
                                    
            // Add the scroll pane to the container
            add(diseaseScroll);
            diseaseScroll.setBounds(0,0,600,230);
      
      }//end constructor
      
      public void displayEditDisease(String sick) //sick being the selected item in the combobox
      {
                  
            Connection conn = null;
            ResultSet rs = null;
            Statement statement = null;
            ResultSetMetaData rsmd;
            int numCols;

      
             try {
                  // Connect to DB
                  Class.forName("com.mysql.jdbc.Driver").newInstance();
                  conn = DriverManager.getConnection("jdbc:mysql:///underwriting",
              "root", "");
      
                  // create a statement, execute query            
                  statement = conn.createStatement();
                  

                  rs = statement.executeQuery("SELECT D_DiseaseCode, D_Description, D_RuleCode, D_Rule, D_Action, D_NumArrow, D_Arrow FROM Disease WHERE" + " D_Description = '" +sick+ "' ORDER BY D_DiseaseCode, D_RuleCode");      

                  ResultSetMetaData metaData = rs.getMetaData();
                  
                  //Get columns
                  int numberOfColumns =  metaData.getColumnCount();
                  columnNames = new Vector();
                  for(int column = 0; column < numberOfColumns; column++)
                  {
                    columnNames.addElement(metaData.getColumnLabel(column+1));
                  }
     
                  // Get all rows.
                  rows = new Vector();
                  while (rs.next())
                  {
                        Vector newRow = new Vector();
                    for (int i = 1; i <= numberOfColumns; i++)
                    {
                            newRow.addElement(rs.getObject(i));
                    }
                    rows.addElement(newRow);
                  }
      
             conn.close();
            statement.close();

            diseaseTable.setModel(new DefaultTableModel(rows, columnNames));

            } catch (SQLException sqle) {
                  //sqle.printStackTrace();
                  System.out.println("+++++"+sqle.getMessage());
            } catch(Exception e) {
            System.err.println("Exception: " + e.getMessage());
            }
            
            //hide 1st 2 columns of table
            diseaseTable.getColumnModel().getColumn(0).setMaxWidth(0);
            diseaseTable.getColumnModel().getColumn(0).setMinWidth(0);
            diseaseTable.getColumnModel().getColumn(1).setMaxWidth(0);
            diseaseTable.getColumnModel().getColumn(1).setMinWidth(0);
            
            diseaseTable.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
            diseaseTable.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
            diseaseTable.getTableHeader().getColumnModel().getColumn(1).setMaxWidth(0);
            diseaseTable.getTableHeader().getColumnModel().getColumn(1).setMinWidth(0);
            
      }//end displayEditDisease
      
      public void actionPerformed(ActionEvent e)
      {

      }//end of actionPerformed
      
}//end diseasePanel
Avatar of sudhakar_koundinya
sudhakar_koundinya

Listening
Avatar of Mayank S
>> conn.close();
>> statement.close();

You should close the statement first and then the connection.
UPDATE Disease SET D_DiseaseCode = 'newValue', D_RuleCode = 'newValue', ....
WHERE D_Description = '" +sick+ "' " ;

?
First you must implement javax.swing.table.TableModel .You can extend AbstractTableModel if you don't want to implement all methods. Implement method setValueAt to store data to DB, and getValueAt to display data at given position.Next you must setup this model via JTable.setModel method.
Avatar of ram817

ASKER

>> UPDATE Disease SET D_DiseaseCode = 'newValue', D_RuleCode = 'newValue', ....
>>WHERE D_Description = '" +sick+ "' " ;

yes, I know how to set up my update sql statement. I just wanted to know how I would go about updating with my JTable and reflecting these changes in my database.

sir albel, if I use the method setValueAt, will I still need and update statement in sql or does the method do that automatically?.
ASKER CERTIFIED SOLUTION
Avatar of albel
albel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ram817

ASKER

thanks sir albel, I'll give it a try and hopefully, I can get it to work.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial