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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.sql.*;
//import com.brainysoftware.java.StringUtil;
public class InesertSQL2 extends HttpServlet
{
private String prenom ="";
private String nom ="";
private String pseudo ="";
private String pass ="";
private String urlConnection = "jdbc:mysql://localhost:3306/frontal";
private String loginConnection = "root";
private String passConnection = "xav";
public void init()
{
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.print("Driver JDBC chargé");
}
catch (ClassNotFoundException e) {
System.out.print(e.toString());
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//sendPageHeader(response);
//sendRegistrationForm(request, response, false);
//sendPageFooter(response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//id = request.getParameter(id);
prenom =request.getParameter(prenom);
nom =request.getParameter(nom);
pseudo =request.getParameter(pseudo);
pass =request.getParameter(pass);
boolean error = false;
String message = null;
try {
Connection cnx = DriverManager.getConnection(urlConnection, loginConnection, passConnection);
System.out.print("Obtenir la connection");
Statement etat = cnx.createStatement();
String sql = new String ("SELECT pseudo from user where pseudo = '" + pseudo + "'");
ResultSet req = etat.executeQuery(sql);
if(req.next())
{
req.close();
message = "Le nom < "+ pseudo + " > existe déjà !";
}
else
{
req.close();
sql = "insert into user" + prenom + nom + pseudo + pass + "VALUES" +
prenom + nom + pseudo + pass ;
int insertion = etat.executeUpdate(sql);
if(insertion != 0)
System.out.print("Merci" + pseudo);
}
// on ferme la requete
etat.close();
// on ferme la connection
cnx.close();
}
catch(SQLException e)
{
message = "Erreur dans la requette" + e.toString() + error;
error = true;
}
if(message != null)
{
PrintWriter sortie = response.getWriter();
sortie.print("<strong>" + message + "</strong>");
}
if(error != false)
{
message = "Erreur dans la requette" + error;
//sendRegistrationForm();
}
response.setContentType("text/html");
PrintWriter sortie = response.getWriter();
sortie.print("<html>");
sortie.print("<head>");
sortie.print("<title>Tableaux</title>");
sortie.print("<style>");
sortie.print("");
sortie.print("</style>");
sortie.print("<link rel='stylesheet' media='screen' href='MonPremierCss.css' />");
sortie.print("</head>");
sortie.print("<body>");
sortie.print("<h2>SQL, Tool</h2>");
sortie.print("<h5>Please type your SQL statement in the following box</h5>");
sortie.print("<form method='post'>");
sortie.print("<textarea name='TexteRequete'>sdfg");
sortie.print("</textarea>");
sortie.print("<submit type='text' name='Execute'>");
sortie.print("</form>");
sortie.print("<table>");
sortie.print("<th>N°</th><th>Prénom</th><th>nom</th><th>Pseudo</th><th>Pass</th>");
sortie.print("<tr>");
sortie.print("<td>qsdfghj</td>");
sortie.print("</tr>");
sortie.print("</table>");
sortie.print("</body>");
}
} |
Partager