Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 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. System.out.println("Nom ?");
  76. String num = stdin.readLine();
  77. boolean ok = false;
  78. stdout.println("Nom du client ?");
  79. String nom = stdin.readLine();
  80. stdout.println("adresse du client ?");
  81. String adresse = stdin.readLine();
  82.  
  83. ajouterClient(db, nom, adresse);
  84.  
  85. } else {
  86. // Si l'utilisateur entre 'h' ou une commande
  87. // inconnue, on lui affiche l'aide
  88. stdout.println(help);
  89. }
  90.  
  91. } catch (Exception e) {
  92. // En cas d'erreur, le programme s'arrête et on affiche le
  93. // message d'erreur
  94. e.printStackTrace(System.out);
  95. }
  96. }
  97. }
  98.  
  99.  
  100. // Point d'entrée du programme
  101. public static void main(String[] args) {
  102. try {
  103. // On récupère le port et le nom de la base ; par défaut, le
  104. // port est 5433 et la base s'appelle TD2
  105. int port = 5432;
  106. String dbname = "TD2";
  107.  
  108. Properties connectionProps = new Properties();
  109. connectionProps.put("user", "sbd");
  110. connectionProps.put("password", "sbd");
  111.  
  112. if (args.length >= 1) {
  113. try {
  114. port = Integer.parseInt(args[0]);
  115. } catch (NumberFormatException e) {
  116. System.out.println("Invalid port number " + args[0] + "; use " + port + " instead");
  117. }
  118. }
  119.  
  120. if (args.length >= 2) {
  121. dbname = args[1];
  122. }
  123.  
  124. // On tente de se connecter à la base de données
  125. Connection db = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + dbname, connectionProps);
  126.  
  127. // On lance l'interface de dialogue avec l'utilisateur
  128. interfaceClient(db);
  129.  
  130. } catch (SQLException e) {
  131. e.printStackTrace(System.out);
  132. }
  133. }
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement