Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Client {
- public static String nom, prenom, adrs;
- private static int id, maxcredit, totalpaye, totalnonpaye;
- //i want data to be stored in this arraylist
- public static ArrayList<ClientInfo> clients = new ArrayList();
- public Client(){
- connectDB();//connecting to the database
- getClients();//storing the data from the database to arraylist!!
- }
- private static Connection conn = null;
- private static Statement stmt = null;
- private static ResultSet rs = null;
- private static final String Conn_string = "jdbc:mysql://localhost/carnetcredit";
- //Connect to DB:
- public static void connectDB(){
- try{
- conn = DriverManager.getConnection(Conn_string, "root", "");
- }catch(SQLException e){
- System.err.println(e);
- }
- }
- //Get all clients list:
- public static void getClients(){
- try{
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery("select * from client");
- System.out.println("Table Client : ");
- while (rs.next()){
- //getting data from database to simpte variables
- id = rs.getInt("idclient");
- nom = rs.getString(2);
- prenom = rs.getString(3);
- adrs = rs.getString(4);
- maxcredit = rs.getInt(5);
- totalpaye = rs.getInt(6);
- totalnonpaye = rs.getInt(7);
- //creating an object using the data extracted from the database
- ClientInfo client = new ClientInfo(id,nom,prenom,adrs,maxcredit,totalpaye,totalnonpaye);
- //adding the object to the arraylist
- clients.add(client);
- }
- }catch(SQLException e){
- System.err.println(e);
- }
- }
- //::::::::::::::::::::::::::::::::::::::::::::::::::: Main Method :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- public static void main(String[] args) {
- conn = null;
- stmt = null;
- rs = null;
- ClientInfo client = new ClientInfo();
- new Client();
- int i = 0;
- while (i<clients.size()){
- client = clients.get(i);
- System.out.println("id : "+ client.id +" - nom : "+client.nom+" - prenom : "+client.prenom+" - adresse : "+client.adrs+
- " - maxcredit : "+client.maxcredit+" - total payé : "+client.totalpaye+" - total non payé : "+client.totalnonpaye);
- i++;
- }
- }
- //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- }
- public class ClientInfo {
- public String nom, prenom, adrs;
- public int id, maxcredit, totalpaye, totalnonpaye;
- public ClientInfo(){
- id = 0;
- nom = prenom = adrs = "";
- maxcredit = totalpaye = totalnonpaye = 0;
- }
- public ClientInfo(int id, String nom, String prenom, String adrs, int maxcredit, int totalpaye,int totalnonpaye){
- this.id = id;
- this.nom = nom;
- this.prenom = prenom;
- this.adrs = adrs ;
- this.maxcredit = maxcredit ;
- this.totalpaye = totalpaye ;
- this.totalnonpaye = totalnonpaye;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement