JAVA - How To Fill A List With MySql Database Values In Java

JAVA - How To Bind A List With MySql Database Data In Java

In this java Tutorial we will see How To Populate a List from MySQL database
In Java NetBeans .



Project Source Code:

package JavaDB_001;
import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.*;
import java.sql.Statement;
import java.awt.List;

public class Work extends JFrame{
  List list;
     public Work(){
         list = new List();
         list.setBounds(20,20,440,420);
         
         BindList();
         
         add(list);
         setLayout(null);
         setSize(500, 500);
        getContentPane().setBackground(Color.decode("#bdb76b"));
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
     }
     


     //create A function To Bind The List With MySql Database
     public void BindList(){
         Connection con;
         Statement st;
         ResultSet rs;
         try{
             con = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");
             st = con.createStatement();
             rs = st.executeQuery("select * from users");
             while(rs.next()){
                 list.add(rs.getString(2));
             }
         }catch(Exception ex){
             ex.printStackTrace();
         }
     }
   
     public static void main(String[] args){
         new Work();
     }
    }


///////////////END


/////OUTPUT:

How To Bind A List With MySql Database Values In Java
Populate A List With MySql Database Values In Java



Share this

Related Posts

Previous
Next Post »