Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.*;
- // En Java, on doit toujours se placer dans une classe
- public class ClientsCorrection {
- // Méthode affichant les données d'un client identifié par son id
- private static void afficherDonneesClient(Connection db, int numero) throws Exception {
- 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;");
- st.setInt(1, numero);
- ResultSet rs = st.executeQuery();
- DBTablePrinter.printResultSet(rs);
- rs.close();
- st.close();
- }
- // Méthode inscrivant un nouveau client, et renvoyer son numéro de client
- private static int ajouterClient(Connection db, String nom, String adresse) throws Exception{
- //Ajout du client
- PreparedStatement st = db.prepareStatement("INSERT INTO clients(nom, adresse VALUES (?, ?))");
- st.setString(1, nom);
- st.setString(2, adresse);
- ResultSet rs = st.executeQuery();
- DBTablePrinter.printResultSet(rs);
- rs.close();
- st.close();
- //recuperation de l'id du client
- PreparedStatement st2 = db.prepareStatement("SELECT id FROM clients AS cl WHERE cl.nom = ? AND cl.adresse = ?"); // commande à déterminer
- st2.setString(1, nom);
- st2.setString(2, adresse); // idem
- ResultSet rs2 = st2.executeQuery();
- rs2.next();
- int idClient = rs2.getInt(1);
- rs2.close();
- st2.close();
- // Renvoi de l'id
- return idClient;
- }
- private static void interfaceClient(Connection db) throws SQLException {
- // Entrée et sortie standard
- PrintWriter stdout = new PrintWriter(System.out, true);
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- // Message d'aide
- String help = "Liste des commandes :\n i: infos d'un client\n q: quitter\n h: obtenir cette aide";
- // On attend en permanence des demandes de l'utilisateur,
- // jusqu'à ce qu'il quitte
- while (true) {
- try {
- // On demande la commande
- stdout.println("Commande ? [aiqh]");
- // On attend la réponse
- String commande = stdin.readLine();
- // On réagit en fonction de cette réponse
- if (commande.equals("i")) {
- System.out.println("Numéro du client ?");
- String num = stdin.readLine();
- int idClient = 1;
- boolean ok = false;
- while (!ok) {
- try {
- idClient = Integer.parseInt(num);
- ok = true;
- } catch (IllegalArgumentException e) {
- stdout.println("Numéro illisible, veuillez recommencer");
- num = stdin.readLine();
- }
- }
- afficherDonneesClient(db, idClient);
- } else if (commande.equals("q")) {
- // On quitte
- break;
- } else if (commande.equals("a")) {
- System.out.println("Nom ?");
- String num = stdin.readLine();
- boolean ok = false;
- while (!ok) {
- try {
- idClient = Integer.parseInt(num);
- ok = true;
- } catch (IllegalArgumentException e) {
- stdout.println("Numéro illisible, veuillez recommencer");
- num = stdin.readLine();
- }
- }
- afficherDonneesClient(db, idClient);
- } else {
- // Si l'utilisateur entre 'h' ou une commande
- // inconnue, on lui affiche l'aide
- stdout.println(help);
- }
- } catch (Exception e) {
- // En cas d'erreur, le programme s'arrête et on affiche le
- // message d'erreur
- e.printStackTrace(System.out);
- }
- }
- }
- // Point d'entrée du programme
- public static void main(String[] args) {
- try {
- // On récupère le port et le nom de la base ; par défaut, le
- // port est 5433 et la base s'appelle TD2
- int port = 5433;
- String dbname = "TD2";
- if (args.length >= 1) {
- try {
- port = Integer.parseInt(args[0]);
- } catch (NumberFormatException e) {
- System.out.println("Invalid port number " + args[0] + "; use " + port + " instead");
- }
- }
- if (args.length >= 2) {
- dbname = args[1];
- }
- // On tente de se connecter à la base de données
- Connection db = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + dbname);
- // On lance l'interface de dialogue avec l'utilisateur
- interfaceClient(db);
- } catch (SQLException e) {
- e.printStackTrace(System.out);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement