Mise à jour/Modifier les données saisies à partir de la base de données de servlet

J'ai préparé un formulaire pour l'insertion, l'affichage et la suppression de données
et maintenant, je veux ajouter une option pour modifier les données, mais soit je suis de mauvaise façon de le faire ou je ne sais pas comment faire.

Donc, quelqu'un peut-il m'aider à procédure pour modifier les données saisies ?? Dois-je donner la référence à d'anciens html de la page pour la modifier ou de la nouvelle page ou .. ??
Je sais que je n'ai pas suivi structure MVC, mais je voulais le faire tout de codage dans une seule page afin d'attente pour obtenir de l'aide..
Merci.

Ce sont mes codes.

Datastore.html

<html>
    <head>
        <title>Data insertion form</title>
    </head>

    <body>

    <form method= "get" action="/datainsert/DataInsertTable">

    <h1><center>Enter the required information</center></h1>

    <table>
        <tr>
            <td>Name</td>
            <td> :- </td>
            <td><input type="text" name="name"></input></td>
        </tr>

        <tr>
            <td>Roll Number</td>
            <td> :- </td>
            <td><input type="text" name="roll"></input></td>
        </tr>

        <tr>
            <td>Class </td>
            <td> :- </td>
            <td><input type="text" name="clas"></input></td>
        </tr>

        <tr>
            <td>Mobile Number </td>
            <td> :- </td>
            <td><input type="text" name="mono"></input></td>
        </tr>

        <tr>
            <td></td>
            <td><input type="submit" value="Submit" width="16" onClick=alert("data stored.");></input></td>
        </tr>

    </table>


    </form>

    </body>
</html>

DataInsertTable.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataInsertTable extends HttpServlet    {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException    {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PrintWriter pwinsert = res.getWriter();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st = null;
out.println("<html>");
out.println("<head>");
out.println("<title>User Data</title>");
out.println("<script language = JavaScript>");
out.println("<function f1() {>");
out.println("<{>");
out.println("<function f1() {>");
out.println("<alert('Select data');>");
out.println("<}>");
out.println("</script>");
out.println("</head>");
out.println("</head>");
out.println("<body>");
out.println("<center><u><h1>User Data</h1></u>");
out.println("<form name='form' >");
out.println("<table border="+2+ "> ");
out.println("<tr>");
out.println("<td> Select </td>");
out.println("<td> Name </td>");
out.println("<td> Roll No. </td>");
out.println("<td> Class </td>");
out.println("<td> Mobile Number </td>");
out.println("</tr>");
String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex)    {
System.out.println("driver not loaded");
System.exit(0);
}
String URL = "jdbc:oracle:thin:@192.168.106.87:1521:ora11g";
String Username = "pratik";
String Password = "pratik";
//Insert                
try {
con = DriverManager.getConnection(URL,Username,Password);
if(req.getParameter("choise")==null)    {
ps = con.prepareStatement("INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ? )");
ps.setString(1,nm); 
ps.setString(2,roll);
ps.setString(3,clas);
ps.setString(4,mono);
int i = ps.executeUpdate();
pwinsert.println(i);
if(i!=0)    {
pwinsert.println("data has been stored");
}
else    {
pwinsert.println("data could not be stored");
}
}
}
catch(Exception e)  {
pwinsert.println(e.getMessage());
}
String idr=null;
//Delete
//cond. nt neeeded (null)
if(req.getParameter("choise")!=null)    {
nm = req.getParameter("choise");
idr = "DELETE from student WHERE name ='"+nm+"'";
try {
st=con.createStatement();
rs = st.executeQuery(idr);
}
catch (Exception e) {
e.getMessage();
System.out.println("Error " +e);
}
System.out.println("Data se deleted..."); 
}
//Update
/*              String updt = "UPDATE student SET name ='"+nm +"',rollno='" + roll + "', class='"+ clas +"', mobileno='"+mono+"' WHERE name ='"+nm +"' ";
PreparedStatement ps1 = null;  
try {
ps1=con.prepareStatement(updt);
} catch (SQLException e2) {
e2.printStackTrace();
}  
int i = 0;
try {
i = ps1.executeUpdate();
} catch (SQLException e1) {
e1.printStackTrace();
}  
if(i==1)  
out.println("success");  
else  
out.println("failed"); 
*/
//edit             
if(req.getParameter("choise")!=null)    {
nm = req.getParameter("choise");
idr = "UPDATE student SET name = ?, rollno = ?, class = ?, mobileno = ? WHERE name = ?";
try {
st=con.createStatement();
int rs1 = st.executeUpdate(idr);
}
catch (Exception e) {
e.getMessage();
System.out.println("Error " +e);
}
System.out.println("Data Edited..."); 
}
else    {
System.out.println("Select row..");
}
/*              String upd = "UPDATE student SET name = ?, rollno = ?, class = ?, mobileno = ? WHERE name = ?"; 
PreparedStatement prest = null;
try {
prest = con.prepareStatement(upd);
} catch (SQLException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}
try {
prest.setString(1,"nm");
} catch (SQLException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}
try {
prest.setString(2,"roll");
} catch (SQLException e2) {
//TODO Auto-generated catch block
e2.printStackTrace();
}
try {
prest.setString(3,"clas");
} catch (SQLException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}
try {
prest.setString(4,"mono");
} catch (SQLException e2) {
//TODO Auto-generated catch block
e2.printStackTrace();
}
try {
prest.executeUpdate();
} catch (SQLException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Updating Successfully!");
*/              
idr = "SELECT * FROM student WHERE name IS NOT NULL";
try {
st=con.createStatement();
rs = st.executeQuery(idr);
}
catch (Exception e) {
e.getMessage();
System.out.println("Error " +e);
}
try {
while (rs.next())   {
out.println("<tr>");
out.println("<td>" + "<input type=\"radio\" name=\"choise\" value=\"" + rs.getString(1) + "\" /> </br>" + "</td>");
out.println("<td>" + rs.getString(1) + "</td>" + "\t <td>" + rs.getInt(2) + "</td>" + "\t <td>" + rs.getString(3) + "</td>" + "\t <td>" + rs.getString(4));
out.println("</tr>");
}
out.println("<tr>");
out.println("<td> </td>");
{                   
out.println("<td>" + "<input type=\"submit\" value=\"Delete Data\" />" + "</td>");
if(req.getParameter("choise") != null)  {
out.println("submitted.");
}
else    {
out.println("<script>alert('Data stored into database')</script>");
}
}
//To Edit 
out.println("<td>" + "<input type=\"button\" value=\"Edit \" onClick = f1() />" + "</td>");
out.println("<td> </td>");
out.println("<td> </td>");
out.println("</tr>");
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
try {
rs.close();
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
try {
st.close();
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
out.println("</table>");
out.println("</form>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
  • encore une fois, quels problèmes rencontrez-vous? ce que l'exception? dans la ligne?
  • lorsque vous faites cela: System.exit(0); vous sont en train de tuer votre propre serveur d'application!!!. Où pouvez-vous définir le "choise" paramètre? pourquoi la banque de données est un html, ne devrait-elle pas être une jsp? sepparate votre code dans plusieurs méthodes: 1 pour la mise à JOUR, 1 pour la SUPPRESSION, 1 fpr d'INSERTION et de 1 pour les SÉLECTIONNER. Si vous supprimez vos enregistrements quand c'est vrai if(req.getParameter("choise")!=null) alors vous aurez jamais les données de mise à jour!!
InformationsquelleAutor Raman | 2013-06-08