From cc9e70fd933cd0de62766238d92d3dc2038cb743 Mon Sep 17 00:00:00 2001 From: "josephine.vetu" Date: Mon, 14 Oct 2024 15:59:20 +0200 Subject: [PATCH] probleme theConnection is null --- src/java/com/test/bdd/ClientMysql.java | 19 ++++++++++--------- src/java/com/test/bdd/Connexion.java | 4 ++-- src/java/gestionClient/NouveauServlet.java | 18 +++++++++++++++++- web/WEB-INF/accueilVue.jsp | 2 +- web/WEB-INF/nouveauClientVue.jsp | 2 +- web/WEB-INF/web.xml | 2 +- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/java/com/test/bdd/ClientMysql.java b/src/java/com/test/bdd/ClientMysql.java index 839c645..13b91ef 100644 --- a/src/java/com/test/bdd/ClientMysql.java +++ b/src/java/com/test/bdd/ClientMysql.java @@ -15,19 +15,19 @@ import javabeans.Client; public class ClientMysql { private final Connection theConnection=Connexion.getConnect("10.121.38.104", "bdclient", "adminBDClient", "mdpBDClient"); + public void create(String nom, String prenom, String mail) throws SQLException{ try { - String sql = "INSERT INTO Client(nom, prenom, mail) VALUES (?, ?, ?)"; + String sql = "INSERT INTO client(nom, prenom, mail) VALUES (?, ?, ?)"; try (PreparedStatement createStmt = theConnection.prepareStatement(sql)) { - createStmt.setString(2, nom); - createStmt.setString(3, prenom); - createStmt.setString(4, mail); + createStmt.setString(1, nom); + createStmt.setString(2, prenom); + createStmt.setString(3, mail); createStmt.executeUpdate(); System.out.println("Un client a été ajouté !"); } } - catch (SQLException ex) { System.out.println("SQLExeption : " + ex.getMessage()); System.out.println("SQLState : " + ex.getSQLState()); @@ -41,7 +41,7 @@ public class ClientMysql { Client leClient = null; try { - String sql = "SELECT * FROM Client WHERE id=?"; + String sql = "SELECT * FROM client WHERE id=?"; try (PreparedStatement readStmt = theConnection.prepareStatement(sql)) { readStmt.setInt(1, id); ResultSet result = readStmt.executeQuery(); @@ -64,7 +64,7 @@ public class ClientMysql { public void update(int id, String nom, String prenom, String mail) throws SQLException{ try { - String sql = "UPDATE Client SET nom=?, prenom=?, mail=? WHERE id=?"; + String sql = "UPDATE client SET nom=?, prenom=?, mail=? WHERE id=?"; try (PreparedStatement updateStmt = theConnection.prepareStatement(sql)) { updateStmt.setInt(1, id); updateStmt.setString(2, nom); @@ -84,9 +84,10 @@ public class ClientMysql { public void delete(int id) throws SQLException{ try { - String sql = "DELETE FROM Client WHERE id=?"; + String sql = "DELETE FROM client WHERE id=?"; PreparedStatement deleteStmt = theConnection.prepareStatement(sql); deleteStmt.setInt(1, id); + deleteStmt.executeQuery(); } catch (SQLException ex) { System.out.println("SQLExeption : " + ex.getMessage()); @@ -101,7 +102,7 @@ public List readAll(){ List lesClients = new ArrayList<>(); // Loop through all IDs and retrieve each client - String sql = "SELECT * FROM Client;"; + String sql = "SELECT * FROM client;"; try (PreparedStatement readStmt = theConnection.prepareStatement(sql)) { ResultSet result = readStmt.executeQuery(); while (result.next()) diff --git a/src/java/com/test/bdd/Connexion.java b/src/java/com/test/bdd/Connexion.java index 8134cd5..61f4563 100644 --- a/src/java/com/test/bdd/Connexion.java +++ b/src/java/com/test/bdd/Connexion.java @@ -36,7 +36,7 @@ public class Connexion { // 3. Connexion connect = (Connection) DriverManager.getConnection(url, user, passwd); - System.out.println("Connexion réussie !"); + System.out.println("Connexion réussie!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !"); } catch (Exception e) { e.printStackTrace(); @@ -52,7 +52,7 @@ public class Connexion { * @return connection établie */ public static Connection getConnect(String serveur, String bdd, String nomUtil, String mdp) { - System.out.println("getConnect"); + System.out.println("getConnect000000000000000000000"); if (connect == null) { new Connexion(serveur, bdd, nomUtil, mdp); } diff --git a/src/java/gestionClient/NouveauServlet.java b/src/java/gestionClient/NouveauServlet.java index 6ddad24..2e297c8 100644 --- a/src/java/gestionClient/NouveauServlet.java +++ b/src/java/gestionClient/NouveauServlet.java @@ -4,12 +4,17 @@ */ package gestionClient; +import com.test.bdd.ClientMysql; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javabeans.Client; /** * @@ -69,7 +74,18 @@ public class NouveauServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - processRequest(request, response); + String nom = request.getParameter("fnom"); + String prenom = request.getParameter("fprenom"); + String mail = request.getParameter("fmail"); + ClientMysql Cli = new ClientMysql(); + try { + Cli.create(nom, prenom, mail); + } catch (SQLException ex) { + Logger.getLogger(NouveauServlet.class.getName()).log(Level.SEVERE, null, ex); + } + + + //processRequest(request, response); } diff --git a/web/WEB-INF/accueilVue.jsp b/web/WEB-INF/accueilVue.jsp index 0a213e0..8a58add 100644 --- a/web/WEB-INF/accueilVue.jsp +++ b/web/WEB-INF/accueilVue.jsp @@ -7,7 +7,7 @@ <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> -ezg + diff --git a/web/WEB-INF/nouveauClientVue.jsp b/web/WEB-INF/nouveauClientVue.jsp index 87c308c..c799af5 100644 --- a/web/WEB-INF/nouveauClientVue.jsp +++ b/web/WEB-INF/nouveauClientVue.jsp @@ -18,7 +18,7 @@ <%@include file="jspf/menuClient.jspf" %>

Nouveau client

Page en construction

-
+

diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index 27bd0bf..9c28b8e 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -2,7 +2,7 @@ ListeServlet - ListeServlet + gestionClient.ListeServlet AccueilServlet