Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. public class Client {
  2. public static String nom, prenom, adrs;
  3. private static int id, maxcredit, totalpaye, totalnonpaye;
  4.  
  5. //i want data to be stored in this arraylist
  6. public static ArrayList<ClientInfo> clients = new ArrayList();
  7.  
  8. public Client(){
  9. connectDB();//connecting to the database
  10. getClients();//storing the data from the database to arraylist!!
  11. }
  12.  
  13. private static Connection conn = null;
  14. private static Statement stmt = null;
  15. private static ResultSet rs = null;
  16.  
  17. private static final String Conn_string = "jdbc:mysql://localhost/carnetcredit";
  18.  
  19.  
  20.  
  21. //Connect to DB:
  22. public static void connectDB(){
  23. try{
  24. conn = DriverManager.getConnection(Conn_string, "root", "");
  25. }catch(SQLException e){
  26. System.err.println(e);
  27. }
  28. }
  29.  
  30. //Get all clients list:
  31. public static void getClients(){
  32. try{
  33.  
  34. stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  35. rs = stmt.executeQuery("select * from client");
  36.  
  37. System.out.println("Table Client : ");
  38.  
  39. while (rs.next()){
  40.  
  41. //getting data from database to simpte variables
  42. id = rs.getInt("idclient");
  43. nom = rs.getString(2);
  44. prenom = rs.getString(3);
  45. adrs = rs.getString(4);
  46. maxcredit = rs.getInt(5);
  47. totalpaye = rs.getInt(6);
  48. totalnonpaye = rs.getInt(7);
  49.  
  50. //creating an object using the data extracted from the database
  51. ClientInfo client = new ClientInfo(id,nom,prenom,adrs,maxcredit,totalpaye,totalnonpaye);
  52.  
  53. //adding the object to the arraylist
  54. clients.add(client);
  55.  
  56. }
  57.  
  58. }catch(SQLException e){
  59. System.err.println(e);
  60. }
  61.  
  62. }
  63.  
  64.  
  65. //::::::::::::::::::::::::::::::::::::::::::::::::::: Main Method :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  66. public static void main(String[] args) {
  67.  
  68. conn = null;
  69. stmt = null;
  70. rs = null;
  71.  
  72.  
  73. ClientInfo client = new ClientInfo();
  74. new Client();
  75. int i = 0;
  76. while (i<clients.size()){
  77. client = clients.get(i);
  78. System.out.println("id : "+ client.id +" - nom : "+client.nom+" - prenom : "+client.prenom+" - adresse : "+client.adrs+
  79. " - maxcredit : "+client.maxcredit+" - total payé : "+client.totalpaye+" - total non payé : "+client.totalnonpaye);
  80. i++;
  81. }
  82. }
  83. //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  84.  
  85. }
  86.  
  87. public class ClientInfo {
  88.  
  89. public String nom, prenom, adrs;
  90. public int id, maxcredit, totalpaye, totalnonpaye;
  91.  
  92.  
  93. public ClientInfo(){
  94. id = 0;
  95. nom = prenom = adrs = "";
  96. maxcredit = totalpaye = totalnonpaye = 0;
  97. }
  98.  
  99. public ClientInfo(int id, String nom, String prenom, String adrs, int maxcredit, int totalpaye,int totalnonpaye){
  100. this.id = id;
  101. this.nom = nom;
  102. this.prenom = prenom;
  103. this.adrs = adrs ;
  104. this.maxcredit = maxcredit ;
  105. this.totalpaye = totalpaye ;
  106. this.totalnonpaye = totalnonpaye;
  107.  
  108. }
  109.  
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement