JAVA - How To Get Data From MySQL Database To LinkedList In Java
In this java Collection Tutorial we will see How To retrieve data from MySQL database
using LinkedList In Java NetBeans .
maybe you need to see:
- connect java to mysql.
- using LinkedList.
*Java Courses : Java Complete Course
Java Swing Course
Project Source Code:
package javaapp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
// create a users class
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 String getFname(){
return this.Fname;
}
public String getLname(){
return this.Lname;
}
public int getAge(){
return this.Age;
}
// create a tostring methode to diplay data
public String ToString(){
return this.Id+" "+this.Fname+" "+this.Lname+" "+this.Age;
}
}
public class Work {
// create a methode to get the connection
public static Connection getConnection(){
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/test_db", "root", "");
} catch (SQLException ex) {
Logger.getLogger(Work.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
public static void main(String[] args){
// create the linkedlist
LinkedList<String> list = new LinkedList<String>();
Users u;
Connection con = getConnection();
Statement st = null;
ResultSet rs = null;
try{
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM users");
while(rs.next()){
Integer id = rs.getInt("id");
String fname = rs.getString("fname");
String lname = rs.getString("lname");
int age = rs.getInt("age");
u = new Users(id, fname,lname,age);
// set data in the linkedlist with our tostring methode
list.add(u.ToString());
}
}catch(Exception ex){
ex.printStackTrace();
}
// display data from the LinkedList
for(String s : list)
System.out.println(s);
// now your data are displayed from mysql database using LinkedList
}
}
using LinkedList In Java NetBeans .
maybe you need to see:
- connect java to mysql.
- using LinkedList.
*Java Courses : Java Complete Course
Java Swing Course
Project Source Code:
package javaapp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
// create a users class
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 String getFname(){
return this.Fname;
}
public String getLname(){
return this.Lname;
}
public int getAge(){
return this.Age;
}
// create a tostring methode to diplay data
public String ToString(){
return this.Id+" "+this.Fname+" "+this.Lname+" "+this.Age;
}
}
public class Work {
// create a methode to get the connection
public static Connection getConnection(){
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/test_db", "root", "");
} catch (SQLException ex) {
Logger.getLogger(Work.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
public static void main(String[] args){
// create the linkedlist
LinkedList<String> list = new LinkedList<String>();
Users u;
Connection con = getConnection();
Statement st = null;
ResultSet rs = null;
try{
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM users");
while(rs.next()){
Integer id = rs.getInt("id");
String fname = rs.getString("fname");
String lname = rs.getString("lname");
int age = rs.getInt("age");
u = new Users(id, fname,lname,age);
// set data in the linkedlist with our tostring methode
list.add(u.ToString());
}
}catch(Exception ex){
ex.printStackTrace();
}
// display data from the LinkedList
for(String s : list)
System.out.println(s);
// now your data are displayed from mysql database using LinkedList
}
}
///////////////OUTPUT:
1 Fuser_1 Luser_1 562 Fuser_2 Luser_2 264 ftest ltest 435 DBB BDD 146 hgjk sdfr 257 some thing 328 white black 429 AAA1 BBB1 3210 WOR HOME 5413 java csharp 6614 ASP.NET JAVAEE 1115 FN LN 40Bagikan
JAVA - How To Populate a LinkedList From MySQL DataBase In Java
4/
5
Oleh
insurance



