Advertisement
Guest User

Untitled

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