probleme theConnection is null

This commit is contained in:
josephine.vetu 2024-10-14 15:59:20 +02:00
parent 8cf65573ae
commit cc9e70fd93
6 changed files with 32 additions and 15 deletions

View File

@ -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<Client> readAll(){
List<Client> 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())

View File

@ -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);
}

View File

@ -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);
}

View File

@ -7,7 +7,7 @@
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
ezg
<!DOCTYPE html>
<html>
<head>

View File

@ -18,7 +18,7 @@
<%@include file="jspf/menuClient.jspf" %>
<h2>Nouveau client</h2>
<p>Page en construction</p>
<form action="action" method="post">
<form action="nouveauClient" method="post">
<label for="nom">Nom : </label>
<input type="text" id="fnom" name="ztNom" required="required"><br><br>
<label for="nom">Prénom : </label>

View File

@ -2,7 +2,7 @@
<web-app version="6.0" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<servlet>
<servlet-name>ListeServlet</servlet-name>
<servlet-class>ListeServlet</servlet-class>
<servlet-class>gestionClient.ListeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>AccueilServlet</servlet-name>