Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3.  
  4.  
  5. // En Java, on doit toujours se placer dans une classe
  6. public class ClientsCorrection {
  7.  
  8.  
  9. // Méthode affichant les données d'un client identifié par son id
  10. private static void afficherDonneesClient(Connection db, int numero) throws Exception {
  11. PreparedStatement st = db.prepareStatement("SELECT cl.nom, cl.adresse, con.commande, con.article, con.quantité FROM clients AS cl, commandes AS com, contenu_commandes AS con WHERE cl.id = ? AND cl.id = com.client AND com.id = con.commande;");
  12. st.setInt(1, numero);
  13. ResultSet rs = st.executeQuery();
  14. DBTablePrinter.printResultSet(rs);
  15. rs.close();
  16. st.close();
  17. }
  18.  
  19. // Méthode inscrivant un nouveau client, et renvoyer son numéro de client
  20. private static int ajouterClient(Connection db, String nom, String adresse) throws Exception{
  21. //Ajout du client
  22. PreparedStatement st = db.prepareStatement("INSERT INTO clients(nom, adresse VALUES (?, ?))");
  23. st.setString(1, nom);
  24. st.setString(2, adresse);
  25. ResultSet rs = st.executeQuery();
  26. DBTablePrinter.printResultSet(rs);
  27. rs.close();
  28. st.close();
  29.  
  30. //recuperation de l'id du client
  31. PreparedStatement st2 = db.prepareStatement("SELECT id FROM clients AS cl WHERE cl.nom = ? AND cl.adresse = ?"); // commande à déterminer
  32. st2.setString(1, nom);
  33. st2.setString(2, adresse); // idem
  34. ResultSet rs2 = st2.executeQuery();
  35. rs2.next();
  36. int idClient = rs2.getInt(1);
  37. rs2.close();
  38. st2.close();
  39.  
  40. // Renvoi de l'id
  41. return idClient;
  42. }
  43.  
  44. private static void interfaceClient(Connection db) throws SQLException {
  45. // Entrée et sortie standard
  46. PrintWriter stdout = new PrintWriter(System.out, true);
  47. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  48.  
  49. // Message d'aide
  50. String help = "Liste des commandes :\n i: infos d'un client\n q: quitter\n h: obtenir cette aide";
  51.  
  52. // On attend en permanence des demandes de l'utilisateur,
  53. // jusqu'à ce qu'il quitte
  54. while (true) {
  55. try {
  56. // On demande la commande
  57. stdout.println("Commande ? [aiqh]");
  58. // On attend la réponse
  59. String commande = stdin.readLine();
  60.  
  61. // On réagit en fonction de cette réponse
  62. if (commande.equals("i")) {
  63. System.out.println("Numéro du client ?");
  64. String num = stdin.readLine();
  65. int idClient = 1;
  66. boolean ok = false;
  67. while (!ok) {
  68. try {
  69. idClient = Integer.parseInt(num);
  70. ok = true;
  71. } catch (IllegalArgumentException e) {
  72. stdout.println("Numéro illisible, veuillez recommencer");
  73. num = stdin.readLine();
  74. }
  75. }
  76. afficherDonneesClient(db, idClient);
  77.  
  78. } else if (commande.equals("q")) {
  79. // On quitte
  80. break;
  81.  
  82. } else if (commande.equals("a")) {
  83. System.out.println("Nom ?");
  84. String num = stdin.readLine();
  85. boolean ok = false;
  86. while (!ok) {
  87. try {
  88. idClient = Integer.parseInt(num);
  89. ok = true;
  90. } catch (IllegalArgumentException e) {
  91. stdout.println("Numéro illisible, veuillez recommencer");
  92. num = stdin.readLine();
  93. }
  94. }
  95. afficherDonneesClient(db, idClient);
  96.  
  97. } else {
  98. // Si l'utilisateur entre 'h' ou une commande
  99. // inconnue, on lui affiche l'aide
  100. stdout.println(help);
  101. }
  102.  
  103. } catch (Exception e) {
  104. // En cas d'erreur, le programme s'arrête et on affiche le
  105. // message d'erreur
  106. e.printStackTrace(System.out);
  107. }
  108. }
  109. }
  110.  
  111.  
  112. // Point d'entrée du programme
  113. public static void main(String[] args) {
  114. try {
  115. // On récupère le port et le nom de la base ; par défaut, le
  116. // port est 5433 et la base s'appelle TD2
  117. int port = 5433;
  118. String dbname = "TD2";
  119.  
  120. if (args.length >= 1) {
  121. try {
  122. port = Integer.parseInt(args[0]);
  123. } catch (NumberFormatException e) {
  124. System.out.println("Invalid port number " + args[0] + "; use " + port + " instead");
  125. }
  126. }
  127.  
  128. if (args.length >= 2) {
  129. dbname = args[1];
  130. }
  131.  
  132. // On tente de se connecter à la base de données
  133. Connection db = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + dbname);
  134.  
  135. // On lance l'interface de dialogue avec l'utilisateur
  136. interfaceClient(db);
  137.  
  138. } catch (SQLException e) {
  139. e.printStackTrace(System.out);
  140. }
  141. }
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement