Populate Database Values in Java List Component using Java Tutorial

0
43

Populate Database Values in Java List Component using Java Tutorial

This tutorial is all about Populate Database Values in Java List Component using Java Tutorial. In this tutorial you will learn how toPopulate Database Values in Java List Component using Java Tutorial. This tutorial uses Netbeans IDE and a Java List Component in Java. Please follow all the steps below to complete this tutorial.

Populate Database Values in Java List Component using Java Tutorialsteps

Create your project by clicking file at the top of your project and then click new project, you can name your project whatever you want.

After that you need to import this package, importjavax.swing.DefaultListModel;above your classand then copy paste the code below:

 

  1. DefaultListModelobjectReference = new DefaultListModel();
  2.  
  3. try{
  4.  
  5. String sql="SELECT firstname FROM users";
  6.  
  7. pst = conn.prepareStatement(sql);
  8.  
  9. rs = pst.executeQuery();
  10.  
  11. while(rs.next()){
  12.  
  13. dlm.addElement(rs.getString("firstname"));
  14.  
  15. }
  16.  
  17. myListView.setModel(dlm);
  18.  
  19. }catch(Exception e){
  20.  
  21. JOptionPane.showMessageDialog(null, e.getMessage());
  22.  
  23. }

Then you’re done. Run your project and see if it works.

In this code we are creating a query statement that filter all the first name from users table and adding it on List element each time the query loops

 

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: [email protected]

Facebook: facebook.com/RyyanSalem

Contact No.: +639098911050

Ryan A. Salem

BSIT Graduate, soon to be MIT.

Java Developer / System Developer

Related topic(s) that you make like:

1.) How to play mp3 music in Java
2.) Create MySQL Connection in Java

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here