Results 1 to 4 of 4
  1. #1
    javatester is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default JAVA JTable rebind

    Good morning all,
    I'm fairly new to java so please have patience. I have a java application that takes values from JTextFields and inserts them into a mySQL database. The below code is from my add button which adds a record to the database and then reloads the DefaultTableModel. If anybody could point me in the right direction of how to rebind the jTable to the jTextFields when the DefaultTableModel is reloaded it would be greatly appreciated.

    Java Code:
    private void testButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    
    try
    {
    String studentID = idField.getText();
    String firstName = fNameField.getText();
    String lastName = LNameField.getText();
    String gpa = gpaField.getText();
    String status = statCombo.getSelectedItem().toString();
    String mentor = mentorField.getText();
    String level = levelCombo.getSelectedItem().toString();
    String thesisTitle = thesisTitleArea.getText();
    String thesisAdvisor = thesisAdvisorField.getText();
    String company = companyField.getText();
    
    Class.forName(driver);
    String url = URL;
    Connection con = DriverManager.getConnection( url,user, pass);
    String sql = "INSERT INTO registrar.students (studentID, firstName, lastName, gpa, status, mentor, level, thesisTitle, thesisAdvisor, company) VALUES (?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement ps = (PreparedStatement) con.prepareStatement( sql );
    ps.setString(1, studentID);
    ps.setString(2, firstName);
    ps.setString(3, lastName);
    ps.setString(4, gpa);
    ps.setString(5, status);
    ps.setString(6, mentor);
    ps.setString(7, level);
    ps.setString(8, thesisTitle);
    ps.setString(9, thesisAdvisor);
    ps.setString(10, company);
    ps.executeUpdate();
    con.close();
      }     catch (ClassNotFoundException ex) {
                Logger.getLogger(TESTFRAMES.class.getName()).log(Level.SEVERE, null, ex);
            }catch (SQLException e){
          e.printStackTrace();
    }
    try {
             Statement stmt;
                        String query;
                        ResultSet rs;
    
                        Object rowData[][] = {{"Student ID", "First Name", "Last Name", "GPA", "Status", "Mentor", "Level", 
    "Thesis Title", "Thesis Advisor", "Company"}};
    
                        Object columnNames[] = {"Student ID", "First Name", "Last Name", "GPA", "Status", "Mentor", 
    "Level", "Thesis Title", "Thesis Advisor", "Company"};
    
                        DefaultTableModel mTableModel = new DefaultTableModel(rowData,columnNames);
    
                        jTable1.setModel(mTableModel);
    
                        try {
                        Class.forName(driver).newInstance();
                        conn = DriverManager.getConnection(URL, user, pass);
                        } catch (Exception events) {
                        System.err.println("Exception: " + events.getMessage());
                        }
    
                        query = "SELECT * FROM registrar.students";
                        stmt = (Statement) conn.createStatement();
                        rs = stmt.executeQuery(query);
    
                        mTableModel.removeRow(0);
    
                        Object[] rows;
                        while (rs.next()) {
                        rows = new Object[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), 
    rs.getString(5),
                        rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10)};
                        mTableModel.addRow(rows);
                        }
                            //throw new UnsupportedOperationException("Not supported yet.");
                        }
                    catch (SQLException ex) {
        }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Why are you rereading all the data from the database?

    If you add a record to the the database then just add the same data to the TableMdoel at the same time.

  3. #3
    javatester is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    Camickr thank you for your time and answer. I'm pretty sure i see what your saying. When I do the insert to the SQL database I can also just add a row to the table model all in the same. I have just one more concern and I think I can call this one answered. When i do the insert to the tablemodel for just that row do i need to call the new DefaultTableModel or just do an insert to the table as it is?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    No need to create a new TableModel. All you are doing is invoking the addRow() method of the existing DefaultTableModel and the table will be updated automatically.

Similar Threads

  1. number_format() and JTable in java
    By sysout in forum New To Java
    Replies: 10
    Last Post: 08-30-2009, 01:15 PM
  2. Sort and filter in Jtable (java ver 1.5 )
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-16-2009, 12:03 PM
  3. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 01:46 PM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Help with JTable in java
    By carl in forum AWT / Swing
    Replies: 3
    Last Post: 08-11-2007, 09:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •