Problème de Cnx dans My SQL à partir de JSP/Servlet
Bonjour tout le monde
mon pfa s'intitule "gestion de compétences" avec JSP/Servlets , et comme je suis débutant en cette technologie, je ne sais pas comment faire pour insérer le code java dans la servlet de sorte que à chaque clique sur le bouton "Envoyer" dans la page jsp, la servlet récupère les champs de texte contenus dans la jsp et les envoie vers la base de données .
Pouvez-vous m'aider?
Merci d'avance
Voici le code que j'ai fait mais ça ne marche pas :s
la servlet:
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
| package pack;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjoutProjet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("driver chargé");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "ouvrir");
System.out.println("connexion ouverte");
String nom = request.getParameter("snom");
String cout = request.getParameter("scout");
String ddebut = request.getParameter("sddebut");
String dfin = request.getParameter("sdfin");
String chef = request.getParameter("schef");
String nbh = request.getParameter("snbh");
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement("insert into bdmp.t_projet values('"+nom+"','"+cout+"','"+ddebut+"','"+dfin+"','"+chef+"','"+nbh+"')");
pstmt.executeUpdate();
} finally {
pstmt.close();
}
conn.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
while (e!=null) {
System.out.println(e.getMessage());
System.out.println("ANSI-92 SQL State:"+e.getSQLState());
System.out.println("Vendor error:"+e.getErrorCode());
e=e.getNextException();
}
}
getServletContext().getRequestDispatcher("/AjoutProjet.jsp").forward(request,response);
} //GET
//POST
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException {
// on passe la main au GET
doGet(request, response);
}
} |
la page 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
| <HTML><HEAD><TITLE>Ajout projet</TITLE><FONT FACE="Arial,Trebuchet MS, Helvetica">
</HEAD>
<BODY TEXT="#330000" BGCOLOR="#FCEFBE" ALINK="#0000FF" VLINK="#990099"><P>
<H3><CENTER>
<FONT COLOR="#0000FF">Ajouter un nouveau projet </FONT>
</CENTER></H3>
<CENTER><HR WIDTH="75%" NOSHADE color=blue><P>
<I>Les champs marques</I><FONT COLOR="#FF0000"><B>*</B></FONT><I>doivent obligatoirement etre renseignes </I>
<P><B>Nom Projet </B><FONT COLOR="#FF0000"><B>* </B></FONT>
<INPUT TYPE=text NAME="snom" VALUE="" SIZE=50>
<P><b>Cout Projet </b><FONT COLOR="#FF0000"><B>* </B></FONT>
<INPUT TYPE=text NAME="scout" VALUE="" SIZE=40>
<P><b>Date debut Projet </b><font color="#FF0000"><b> </b></font>
<font color="#FF0000"><b>*</b></font>
<input type="text" name="sddebut" value="" size="40" />
<p><b>Date Fin Projet </b><font color="#FF0000"><b> </b></font> <font color="#FF0000"><b>*</b></font>
<input type="text" name="sdfin" value="" size="40" />
</p>
<p><b>Chef Projet </b><font color="#FF0000"><b> </b></font> <font color="#FF0000"><b>*</b></font>
<input type="text" name="schef" value="" size="40" />
</p>
<p><b>Nombre d'heure Projet </b><font color="#FF0000"><b> </b></font> <font color="#FF0000"><b>*</b></font>
<input type="text" name="snbh" value="" size="40" />
</p>
<P>
<input type="button" value="Envoyer" onclick="envoyer()">
<INPUT TYPE=reset NAME=Effacer VALUE="Effacer" >
</CENTER>
<HR WIDTH="75%" NOSHADE color=blue>
</BODY></HTML> |