JAVA - How To Filling List With Database Values In Java Using NetBeans
__________________________________________________________________________
In This Java Code We Will See How To Populate A List With MySQL DataBase Values In Java Programming Language.
In This Java Code We Will See How To Populate A List With MySQL DataBase Values In Java Programming Language.
//////////////////////////////////////////// User Class
package javadb_001;
public class Users {
private int id;
private String fname;
private String lname;
private int age;
public Users(){}
public Users(int _id,String _fname,String _lname,int _age){
this.id = _id;
this.fname = _fname;
this.lname = _lname;
this.age = _age;
}
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public String getFname(){
return this.fname;
}
public void setFname(String fname){
this.fname = fname;
}
public String getLname(){
return this.lname;
}
public void setLname(String lname){
this.lname = lname;
}
public int getAge(){
return this.age;
}
public void setAge(int age){
this.age = age;
}
public String showInfo(){
return this.id+" - "+this.fname+" - "+this.lname+" - "+this.age;
}
}
///////////////////////////////////////////////////////////////////End User Class
_________________________________________________________________________________
//////////////////////////////////////////////////////////////// WorkClass Class
package javadb_001;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class WorkClass {
//create a function to get the connection
public static Connection getConnection()
{
Connection cn;
try{
cn = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");
return cn;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
//create a function to populate a List of Users and return this List
//create a function to populate a List of Users and return this List
public static List<Users> BindList(){
try{
Connection con = getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM users");
List<Users> list = new ArrayList<Users>();
while(rs.next()){
Users u = new Users(Integer.parseInt(rs.getString("id")),
rs.getString("fname"),
rs.getString("lname"),
Integer.parseInt(rs.getString("age"))
);
list.add(u);//add user to the list
}
return list;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
//create a function to show users list data
public static void PrintInfo(){
System.out.println(BindList().size());
for(int i = 0;i < BindList().size();i++){
System.out.println(BindList().get(i).showInfo());
}
}
public static void main(String[] args){
WorkClass.PrintInfo();
}
}
//////////////////////////////////////////////////////////// End WorkClass
//////////////////////////////////////////////////////////// OUTPUT:
//////////////////////////////////////////////////////////// OUTPUT:
12
1 - Fuser_1 - Luser_1 - 56
2 - Fuser_2 - Luser_2 - 26
4 - ftest - ltest - 43
5 - DBB - BDD - 14
6 - hgjk - sdfr - 25
7 - some - thing - 32
8 - white - black - 42
9 - AAA1 - BBB1 - 32
10 - WOR - HOME - 54
13 - java - csharp - 66
14 - ASP.NET - JAVAEE - 11
15 - FN - LN - 40
1 - Fuser_1 - Luser_1 - 56
2 - Fuser_2 - Luser_2 - 26
4 - ftest - ltest - 43
5 - DBB - BDD - 14
6 - hgjk - sdfr - 25
7 - some - thing - 32
8 - white - black - 42
9 - AAA1 - BBB1 - 32
10 - WOR - HOME - 54
13 - java - csharp - 66
14 - ASP.NET - JAVAEE - 11
15 - FN - LN - 40