Creation de clients
This commit is contained in:
@@ -6,6 +6,7 @@ package com.test.bdd;
|
||||
|
||||
import com.test.beans.Client;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
@@ -52,4 +53,33 @@ public class ClientMysql {
|
||||
|
||||
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 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) {
|
||||
this.id = id;
|
||||
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;
|
||||
|
||||
import com.test.forms.NouveauClientForm;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -60,6 +61,7 @@ public class NouveauServlet extends HttpServlet {
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
// processRequest(request, response);
|
||||
request.setAttribute("new_client", false);
|
||||
getServletContext().getRequestDispatcher("/WEB-INF/nouveauClientVue.jsp").forward(request, response);
|
||||
}
|
||||
|
||||
@@ -74,7 +76,15 @@ public class NouveauServlet extends HttpServlet {
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user