bonjour;
j'ai un petit problème d'insertion dans la BD;mais je ne sais pas c'est quoi ce problème d'après mes connaissances le code que j'ai écrit est correcte;
voici le servlet qui fait l'insertion:
[QUOTE]
package Db_cnx;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Cnx_srv extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection cnx=null;
private PreparedStatement instruction =null;
private String req="insert into produits values(?,?,?,?,?,?)";
public void init() throws ServletException{
String pilote =getInitParameter("jdbc");
String db =getInitParameter("localisation");
try {
Class.forName(pilote);
Connection cnx=DriverManager.getConnection(db,"root","");
instruction = cnx.prepareStatement(req);
}
catch (SQLException e)
{
System.out.println("erreur1111");
} catch (ClassNotFoundException e) {
System.out.println("erreur22222");
}
}
public Cnx_srv() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
PrintWriter out = response.getWriter();
System.out.println(Integer.valueOf(request.getParameter("qte")));
instruction.setString(1,request.getParameter("ref"));
instruction.setString(2,request.getParameter("desig"));
instruction.setString(3,request.getParameter("parcourir"));
instruction.setDouble(4,Double.valueOf(request.getParameter("pu")));
instruction.setInt(5,Integer.valueOf(request.getParameter("qte")));
instruction.setString(6,request.getParameter("sel_ref_cat"));
instruction.executeUpdate();
response.setContentType("text/html");
out.println("<html><head><title>Reponse formulaire</title></head><body>");
out.println("<H2>Enregistrement effectué</h2>");
out.println("</body></html>");
}
catch (SQLException e){System.out.println("non enregistrer");}
}
public void destroy(){
if(cnx!=null){
try {
cnx.close();
}
catch(SQLException e){
System.out.println("erreur");
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
voici le code jsp:
voici le web.xml:<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Information Produit</title>
</head>
<body>
<FORM METHOD="GET" ACTION="Cnx_srv">
<table>
<tr>
<td>
Reference Produit:
</td>
<td>
<input type=text name="ref" value="Entrer un Reference">
</td>
</tr>
<tr>
<td>
Designation produit:
</td>
<td>
<input type=text name="desig">
</td>
</tr>
<tr>
<td>
Image produit:
</td>
<td>
<input type="file" name="parcourir" value="Brows">
</td></tr>
<tr>
<td>
Prix Unitaire produit:
</td>
<td>
<input type=text name="pu" value="Prix Unitaire">
</td></tr>
<tr>
<td>
Quantité produit:
</td>
<td>
<input type=text name="qte" value="Quantité">
</td></tr>
<tr>
<td>
Reference Categorie:
</td>
<td>
<select name="sel_ref_cat"">
<option value="cat1" SELECTED >cat1</option>
<option value="cat2">cat2</option>
<option value="cat3">cat3</option>
</select>
</td></tr>
<tr><td>
<input type="submit" value="Enregistrer">
</td></tr>
</table>
</FORM>
</body>
</html>
merci d'avance<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlnssi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Ecommerce</display-name>
<servlet>
<description>
</description>
<display-name>Cnx_srv</display-name>
<servlet-name>Cnx_srv</servlet-name>
<servlet-class>Db_cnx.Cnx_srv</servlet-class>
<init-param>
<param-name>jdbc</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>localisation</param-name>
<param-value>jdbc:mysql://localhost/db_ecom</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Cnx_srv</servlet-name>
<url-pattern>/Cnx_srv</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Partager