Display Data from Selected Table Row to another Form in Java

0
105

This tutorial entitled “Display Data from Selected Table Row to another Form in Java” will help you on how your program display information from selected Table Row and display it to another form in Java using any element that support text.

To complete this tutorial, you are required to learn and understand my last topic about “How to Load Data from MySQL Database to Table Element in Java”. Please follow all the steps to complete this tutorial.

Display Data from Selected Table Row to another Form in Java Steps

  1. Add a new button inside your form that loads data from MySQL Database.

2. The form should look like the image below.

3. Double click the button and insert the ff. codes below.

  1. int row=jTable1.getSelectedRow();
  2. String id = (jTable1.getModel().getValueAt(row, 0).toString());
  3. DataUpdate du = new DataUpdate();
  4. du.loadInformation(id);
  5. du.setVisible(true);

DataUpdate – the name of your form create in Step 4, loadInformation – the name of the method inserted in step 6, jTable1 – the name of your Table.

4. Add/Insert a new JFrame Form into your project.

5. Design your form just like what we do in our Save Data tutorial.

6. Inside your code view, insert the ff. codes below inside your class.

  1. public void loadInformation(String id){
  2. try{
  3. String sql="SELECT * FROM userinfo WHERE idUserInfo='" + id + "'";
  4. pst=conn.prepareStatement(sql);
  5. rs = pst.executeQuery(sql);
  6. while(rs.next())
  7. {
  8. jTextField1.setText(rs.getString("FirstName"));
  9. jTextField2.setText(rs.getString("LastName"));
  10. jTextField3.setText(rs.getString("Address"));
  11. jTextField4.setText(rs.getString("Contact"));
  12. jTextField5.setText(rs.getString("Email"));
  13. }
  14. }
  15. catch(Exception e) {
  16. JOptionPane.showMessageDialog(this, e.getMessage());
  17. }
  18. }

7. Run your load data form and the output should look like the image below. After I click the update button, the new Create form displayed together with the information selected from load data table.

Related Articles You May Like:

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here