ajout a partir d'un formulaire
bonjour tout le monde,
je voudrais faire un ajout d'un client a partir d'un formulaire ou je saisis les informations relatives a ma table client (nom, prenom , adresse, tel) mais le probleme c'est que les données ne se transmettent pas a ma base de donnée les champs entrés en comme valeur 'null'.
voila le code
Client.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package com.connexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Client {
private Integer id_client;
private String nom;
private String prenom;
private String adresse;
private String tel;
String pilote = "com.mysql.jdbc.Driver";
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public Integer getId_client() {
return id_client;
}
public void setId_client(Integer id_client) {
this.id_client = id_client;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String ajoutClient(){
Client c=new Client();
try{
Class.forName(pilote);
Connection connexion = DriverManager.getConnection("Jdbc:mysql://localhost/base","root","");
Statement instruction = connexion.createStatement();
String sql = "INSERT INTO CLIENT (nom,prenom,adresse,tel) VALUES ('"+ c.getNom() + "','" + c.getPrenom() + "','"+ c.getAdresse() + "','"+ c.getTel() + "') ";
instruction.executeUpdate(sql);
}
catch (Exception e){
System.out.println("erreur : "+e.getMessage());
}
return "Success";
}
} |
ajoutClient.jsp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <%@ page language="java" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Ajout client</title>
</head>
<body>
<f:view>
<h:form >
<h:panelGrid columns="2" border="1">
<h:outputText value="Nom:" />
<h:inputText value="#{clientBean.nom}">
</h:inputText>
<h:outputText value="Prenom:" />
<h:inputText value="#{clientBean.prenom}">
</h:inputText>
<h:outputText value="Adresse:" />
<h:inputText value="#{clientBean.adresse}">
</h:inputText>
<h:outputText value="Tel:" />
<h:inputText value="#{clientBean.tel}">
</h:inputText>
</h:panelGrid>
<h:commandButton value="Ajouter"
action="#{clientBean.ajoutClient}"/>
</h:form>
</f:view>
</body>
</html> |
faces-config.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>clientBean</managed-bean-name>
<managed-bean-class>com.connexion.Client</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>ajoutClient.jsp</from-view-id>
<navigation-case>
<from-outcome>Success</from-outcome>
<to-view-id>/listerclient.faces</to-view-id>
</navigation-case>
</navigation-rule> |
merci pour votre aide