Affichage des articles dont le libellé est LinkedList. Afficher tous les articles
Affichage des articles dont le libellé est LinkedList. Afficher tous les articles

Java Populate JTable From LinkedList

How To Fill A JTable With Data From LinkedList Using Java NetBeans

Populate JTable From  LinkedList Using Java


In this Java Tutorial we will see How To Populate A JTable With A Linked List Of Object By Creating A User Class Using For Loop And DefaultTableModel In Java NetBeans .



Project Source Code:


// create a class to use with the linkedList
    class User{
        
        private String firstName;
        private String lastName;
        private int age;
        
        public User(String fn, String ln, int ag){
            this.firstName = fn;
            this.lastName = ln;
            this.age = ag;
        }
        
    }
    
    
    
    public void populateTableWithLinkedList(){
        
        // create a user linkedList
        LinkedList<User> list = new LinkedList<>();
        
        // create users
        User u1 = new User("AA","BB",10);
        User u2 = new User("BB","CC",20);
        User u3 = new User("CC","DD",30);
        User u4 = new User("DD","EE",40);
        User u5 = new User("EE","FF",50);
        User u6 = new User("LL","MM",80);
        User u7 = new User("NN","TT",100);
        
        // add the users to the list
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        list.add(u6);
        list.add(u7);
        
        // get jtable default model
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        
        // populate the jtable with the list
        Object[] row;
        for(int i = 0; i < list.size(); i++){
            row = new Object[3];
            row[0] = list.get(i).firstName;
            row[1] = list.get(i).lastName;
            row[2] = list.get(i).age;
            
            model.addRow(row);
        }
    }

OutPut:

Fill JTable From LinkedList In Java



JAVA - How To Populate a LinkedList From MySQL DataBase In Java

JAVA - How To Get Data From MySQL Database To LinkedList In Java

                                                                                                           

java linkedlist with mysql database

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.


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 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






JAVA - How To Use LinkedList In Java NetBeans

JAVA - How To Use LinkedList In Java NetBeans

Java - How To Display Data From A LinkedList In Java

                                                                                                       

In this java Collection tutorial we will see How To Use A LinkedList In Java NetBeans
1: how to create a linkedlist
2: how to dispaly data from it
3: how to add an elements in the first
4: how to add an elements in the end
5: how to clear all elements from it
and more........



Source Code:

package javaapp;

import java.util.LinkedList;

public class Woirk {

    public static void main(String[] args){
        
        LinkedList<Integer> list = new LinkedList<Integer>();
        LinkedList<Integer> list2 = new LinkedList<Integer>();
        LinkedList<Integer> list3 = new LinkedList<Integer>();
        //add element to the end of the list
        list.add(6);
        list.add(2);
        list.add(8);
        list.add(5);
        list.add(7);
        list.add(8);
        list.add(16);
        list.add(12);
        list.add(8);
        list.add(15);
        list.add(17);
        list.add(8);
        System.out.println(list);
        
        //add element to list2
        list2.add(100);
        list2.add(1000);
        list2.add(10000);
       
        //add element to list3
        list3.add(11111);
        list3.add(33333);
        list3.add(44444);
        
        //add element at the specified position in the list
        list.add(1,10);
        System.out.println(list);
        
        //add collection to the and of another collection
        list.addAll(list2);
        System.out.println(list);
        
 //add collection to another collection in the specified position
        list.addAll(4, list3);
        System.out.println(list);
        
        //add element in the beginning of the list 
        list.addFirst(0);
        System.out.println(list);
        
        //add element in the end of the list
        list.addLast(999999);
        System.out.println(list);
        
        //clone the list
        LinkedList<Integer> listClone = new LinkedList<Integer>();
        listClone = (LinkedList<Integer>) list.clone();
        System.out.println(listClone);
        
        //check if the given element exist in the list
        boolean exist = list.contains(8);
        System.out.println(exist);

        //5 way to get the first element of the linkedlist
        System.out.println(list.element());
        System.out.println(list.getFirst());
        System.out.println(list.get(0));
        System.out.println(list.peek());
        System.out.println(list.peekFirst());
        
        //3 way to get the last element of the linkedlist
        System.out.println(list.getLast());
        System.out.println(list.get(list.size()-1));
        System.out.println(list.peekLast());
        
       //return the index of the element
        int index = list.indexOf(8);
        System.out.println(index);
        
       //return the last index of the element 
        int lastIndex = list.lastIndexOf(8);
        System.out.println(lastIndex);
        
        //insert element at the beginning of the list
        list.offerFirst(2015);
        //insert element at the end of the list
        list.offerLast(500000);
        System.out.println(list);
        
        //5 way to remove the first element in the list
        list.poll();
        list.pollFirst();
        list.remove();
        list.removeFirst();
        list.remove(0);
        System.out.println(list);
        
        //3 way to remove the first element in the list
        list.pollLast();
        list.removeLast();
        list.remove(list.size()-1);
        System.out.println(list);
        
      //delete the first element of a list who is equal to the given element
        list.removeFirstOccurrence(8);
        System.out.println(list);
       // you can also use
       //list.remove(8);
        
    //delete the last element of a list who is equal to the given element
        list.removeLastOccurrence(8);
        System.out.println(list);
        
        //replace element in the given index with the given element
        list.set(0, 123456789);
        System.out.println(list);
        
        //how to use toArray() methode 
        Object[] obj = list.toArray();
        for(Object o : obj)
            System.out.print(o+" - ");
        System.out.println();
        
        //how to use toArray(T[] a) methode 
        Integer[] t = list.toArray(new Integer[0]);
          for(Integer i : t)
            System.out.print(i+" - ");
        System.out.println();
        
        System.out.println("List Size Before The Clear: "+list.size());
        //clear all elements in the list
        list.clear();
        System.out.println("List Size After The Clear: "+list.size());
        

    }
}


//OUTPUT:

[6, 2, 8, 5, 7, 8, 16, 12, 8, 15, 17, 8]
[6, 10, 2, 8, 5, 7, 8, 16, 12, 8, 15, 17, 8]
[6, 10, 2, 8, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000, 10000]
[6, 10, 2, 8, 11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000, 10000]
[0, 6, 10, 2, 8, 11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000, 10000]
[0, 6, 10, 2, 8, 11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000, 10000, 999999]
[0, 6, 10, 2, 8, 11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000, 10000, 999999]
true
0
0
0
0
0
999999
999999
999999
4
16
[2015, 0, 6, 10, 2, 8, 11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000, 10000, 999999, 500000]
[8, 11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000, 10000, 999999, 500000]
[8, 11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000]
[11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 8, 100, 1000]
[11111, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 100, 1000]
[123456789, 33333, 44444, 5, 7, 8, 16, 12, 8, 15, 17, 100, 1000]
123456789 - 33333 - 44444 - 5 - 7 - 8 - 16 - 12 - 8 - 15 - 17 - 100 - 1000 -
123456789 - 33333 - 44444 - 5 - 7 - 8 - 16 - 12 - 8 - 15 - 17 - 100 - 1000 -
List Size Before The Clear: 13
List Size After The Clear: 0