Creation de clients
This commit is contained in:
@@ -6,6 +6,7 @@ package com.test.bdd;
|
|||||||
|
|
||||||
import com.test.beans.Client;
|
import com.test.beans.Client;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
@@ -52,4 +53,33 @@ public class ClientMysql {
|
|||||||
|
|
||||||
return clients;
|
return clients;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int create(Client unClient){
|
||||||
|
int id=-1;
|
||||||
|
String sql = "INSERT INTO client (nom, prenom, mail) VALUES (?,?,?);";
|
||||||
|
|
||||||
|
try (PreparedStatement prepStmt = laConnexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||||
|
|
||||||
|
prepStmt.setString(1, unClient.getNom());//le param. 1 est remplacé par le nom du client
|
||||||
|
prepStmt.setString(2, unClient.getPrenom());
|
||||||
|
prepStmt.setString(3, unClient.getMail());
|
||||||
|
|
||||||
|
int status = prepStmt.executeUpdate();
|
||||||
|
// récupération de la clé auto-générée
|
||||||
|
if (status > 0) {
|
||||||
|
try (ResultSet result = prepStmt.getGeneratedKeys()) {
|
||||||
|
if (result.next()) { // <-- utiliser next() et pas first()
|
||||||
|
id = result.getInt(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prepStmt.close();
|
||||||
|
|
||||||
|
} catch(SQLException ex){
|
||||||
|
System.out.println("SQLException:"+ex.getMessage());
|
||||||
|
System.out.println("SQLState:"+ex.getSQLState());
|
||||||
|
System.out.println("code Erreur:"+ex.getErrorCode());
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,12 @@ public class Client {
|
|||||||
private String prenom;
|
private String prenom;
|
||||||
private String mail;
|
private String mail;
|
||||||
|
|
||||||
|
public Client(String nom, String prenom, String mail) {
|
||||||
|
this.nom = nom;
|
||||||
|
this.prenom = prenom;
|
||||||
|
this.mail = mail;
|
||||||
|
}
|
||||||
|
|
||||||
public Client(int id, String nom, String prenom, String mail) {
|
public Client(int id, String nom, String prenom, String mail) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.nom = nom;
|
this.nom = nom;
|
||||||
|
|||||||
34
src/main/java/com/test/forms/NouveauClientForm.java
Normal file
34
src/main/java/com/test/forms/NouveauClientForm.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||||
|
*/
|
||||||
|
package com.test.forms;
|
||||||
|
|
||||||
|
import com.test.bdd.ClientMysql;
|
||||||
|
import com.test.beans.Client;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author steve.maingana
|
||||||
|
*/
|
||||||
|
public class NouveauClientForm {
|
||||||
|
public String resultat;
|
||||||
|
|
||||||
|
public NouveauClientForm() {};
|
||||||
|
|
||||||
|
public String getResultat() {
|
||||||
|
return resultat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResultat(String resultat) {
|
||||||
|
this.resultat = resultat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int verifierClient(HttpServletRequest request) {
|
||||||
|
Client client = new Client(request.getParameter("nom_client"), request.getParameter("prenom_client"), request.getParameter("mail_client"));
|
||||||
|
ClientMysql clientDB = new ClientMysql();
|
||||||
|
int nouveauClient = clientDB.create(client);
|
||||||
|
return nouveauClient;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.test.servlets;
|
package com.test.servlets;
|
||||||
|
|
||||||
|
import com.test.forms.NouveauClientForm;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
@@ -60,6 +61,7 @@ public class NouveauServlet extends HttpServlet {
|
|||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
// processRequest(request, response);
|
// processRequest(request, response);
|
||||||
|
request.setAttribute("new_client", false);
|
||||||
getServletContext().getRequestDispatcher("/WEB-INF/nouveauClientVue.jsp").forward(request, response);
|
getServletContext().getRequestDispatcher("/WEB-INF/nouveauClientVue.jsp").forward(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +76,15 @@ public class NouveauServlet extends HttpServlet {
|
|||||||
@Override
|
@Override
|
||||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
processRequest(request, response);
|
|
||||||
|
NouveauClientForm nouveauClient = new NouveauClientForm();
|
||||||
|
int idClient = nouveauClient.verifierClient(request);
|
||||||
|
request.setAttribute("new_client", idClient > -1);
|
||||||
|
if (idClient > -1) {
|
||||||
|
response.sendRedirect("/Test2Jakarta/ListeClients");
|
||||||
|
} else {
|
||||||
|
getServletContext().getRequestDispatcher("/WEB-INF/nouveauClientVue.jsp").forward(request, response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
Author : steve.maingana
|
Author : steve.maingana
|
||||||
--%>
|
--%>
|
||||||
|
|
||||||
|
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||||
<%@page contentType="text/html" pageEncoding="UTF-8"%>
|
<%@page contentType="text/html" pageEncoding="UTF-8"%>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
@@ -11,6 +12,22 @@
|
|||||||
<body>
|
<body>
|
||||||
<%@include file="jspf/menu.jspf" %>
|
<%@include file="jspf/menu.jspf" %>
|
||||||
<h1>Création d’un nouveau client</h1>
|
<h1>Création d’un nouveau client</h1>
|
||||||
<p>Page en construction</p>
|
<form method="post" action="NouveauClient">
|
||||||
|
<fieldset>
|
||||||
|
<legend>Client :</legend>
|
||||||
|
<label>Nom :</label>
|
||||||
|
<input type="text" name="nom_client">
|
||||||
|
<label>Prénom :</label>
|
||||||
|
<input type="text" name="prenom_client">
|
||||||
|
<label>Adresse mail :</label>
|
||||||
|
<input type="email" name="mail_client">
|
||||||
|
<input type="submit" value="Compléter">
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
<c:choose>
|
||||||
|
<c:when test="${new_client}">
|
||||||
|
<p>Le client No. ${id_client} a été créé avec succès !</p>
|
||||||
|
</c:when>
|
||||||
|
</c:choose>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user