-1

我需要你的一点帮助,我已经重新编辑了我的代码并再次发布在这里,我想通过单击 EDIT 按钮来编辑数据。任何人都可以帮我一点,如何链接该编辑功能?

索引.jsp

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

    <body>

    <%
    String action="/SimpleServlet/DataInsertTable";
    String method="get";

    if(request.getAttribute("EDIT")!=null)  {
        action="/SimpleServlet/DataUpdateTable";
        method="post";
    }
    %>

    <form method= "<%=method%>" action="<%=action%>">
        <h1><center>Enter the required information</center></h1>

        <table>
            <tr>
                <td>Name</td>
                <td> :- </td>
                <%if(request.getAttribute("NAME")!=null){ %>
                <td><input type="text" name="name" value="<%=request.getAttribute("NAME")%>"></input>

                <%}else{ %>
                <td><input type="text" name="name"></input>
                <%} %>
                </td>
        </tr>

        <tr>
            <td>Roll Number</td>
            <td> :- </td>
            <%if(request.getAttribute("ROLL")!=null){ %>
            <td><input type="text" name="roll" value="<%=request.getAttribute("ROLL")%>"></input>

            <%}else{ %>
            <td><input type="text" name="roll"></input>
            <%} %>
            </td>       
        </tr>

        <tr>
            <td>Class </td>
            <td> :- </td>
             <%if(request.getAttribute("CLAS")!=null){ %>
            <td><input type="text" name="clas" value="<%=request.getAttribute("CLAS")%>"></input>

            <%}else{ %>
            <td><input type="text" name="clas"></input>
            <%} %>
            </td>
        </tr>

        <tr>
            <td>Mobile Number </td>
            <td> :- </td>
            <%if(request.getAttribute("MONO")!=null){ %>
            <td><input type="text" name="mono" value="<%=request.getAttribute("MONO")%>"></input>

            <%}else{ %>
            <td><input type="text" name="mono"></input>
            <%} %>
            </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="submit" value="Submit" width="100%"></input></td>
        </tr>

    </table>

    </form>

    </body>
</html>

数据插入表.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("</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("<td> Edit </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";

            try {
                con = DriverManager.getConnection(URL,Username,Password); 

//              if(req.getParameter("nm")==null && req.getParameter("roll")==null && req.getParameter("clas")==null && req.getParameter("mono")==null) {


// Insert
                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("Your data has been stored in the database"); 
                    } 
                    else    { 
                        pwinsert.println("Your data could not be stored in the database"); 
                    } 
                }
            } 
            catch(Exception e)  {
                pwinsert.println(e.getMessage());

            } 



// Delete           

            String idr=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 deleted..."); 
            }


//          res.sendRedirect("DataInsertTable.java"); 


            idr = "SELECT * FROM student WHERE name IS NOT NULL ORDER BY name"; 

            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("<td>" + "<a href='"+req.getContextPath()+"/DataUpdate?mode=EDIT&name="+rs.getString(1)+"&roll="+rs.getInt(2)+"&clas="+rs.getString(3) +"&mono="+rs.getString(4)+"'>Edit</a> </br>" + "</td>"); 
                    out.println("</tr>"); 
                } 

            } 
            catch (Exception e) { 
                e.getMessage(); 
                System.out.println("Error" +e); 
            } 



            out.println("");
            out.println("<tr>"); 
            out.println("<td> </td>"); 

            out.println("<td>" + "<form>" + "<input type=\"button\" name = \"nw\" value=\"Add More Users\" onClick=\"window.location.href='"+ req.getContextPath()+"/index.jsp'\">" + "<form>" + "<td>");


            out.println("<td>" + "<form>" + "<input type=\"button\" name = \"edit\" value=\"Edit userdata\" onClick=\"window.location.href='"+ req.getContextPath()+"/DataUpdateTable'\">" + "<form>" + "<td>");


//          out.println("<td>" + "<input type=\"submit\" value = \"Edit\">" + "<a href='/SimpleServlet/'></a> </br>" + "</input>" + "</td>");


            out.println("<td>" + "<input type=\"submit\" name=\"del\" value=\"Delete\" />" + "</td>");

            out.println("<td>" + "<input type=\"submit\" value=\"Update\" />" + "</td>");

            out.println("</tr>"); 





            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(); 

    } 
}

数据更新表.java

import java.io.*; 
import java.sql.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class DataUpdateTable 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("</head>");

            out.println("<body>"); 
            out.println("<center><u><h1>User Data</h1></u>"); 

            out.println("<form name='form' >"); 

//          int a = 2; 
            out.println("<table border="+2+ "> "); 
            out.println("<tr>");
                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");

            req.setAttribute("NAME", nm);
            req.setAttribute("ROLL", roll);
            req.setAttribute("CLAS", clas);
            req.setAttribute("MONO", mono);
            req.setAttribute("EDIT", "Y");

            req.getRequestDispatcher("/index.jsp").forward(req, res);

    }


    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException    {

        res.setContentType("text/html"); 
        PrintWriter out = res.getWriter();
        PrintWriter pwinsert = res.getWriter();
//      PrintWriter pwdelete = res.getWriter();

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null; 
        Statement st = null; 


        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";

        try { 
            con = DriverManager.getConnection(URL,Username,Password); 

            String nm = req.getParameter("name"); 
            String roll = req.getParameter("roll"); 
            String clas = req.getParameter("clas"); 
            String mono = req.getParameter("mono"); 


            if(req.getParameter("choise")==null)    {
                ps = con.prepareStatement("update student set name=?, rollno=?, class=?, mobileno=? where rollno=?"); 
                ps.setString(1,nm); 
                ps.setString(2,roll); 
                ps.setString(3,clas); 
                ps.setString(4,mono); 
                ps.setString(5,roll); 

                int i = ps.executeUpdate(); 
                pwinsert.println(i); 


                if(i!=0)    {
                    pwinsert.println("Your data has been stored in the database"); 
                } 
                else    { 
                    pwinsert.println("Your data could not be stored in the database"); 
                } 

                res.sendRedirect( req.getContextPath()+ "/DataInsertTable");
//              res.sendRedirect("https://www.google.co.in/");

            }
        } 
        catch(Exception e)  { 
            pwinsert.println(e.getMessage()); 
        } 
    }
}
4

2 回答 2

0

第一次进入 jsp 时,您打印表单并显示您的字段没有太多麻烦(除了结构问题)。但是,在 servlet 中,您直接打印您的 html,正如我在之前的评论中告诉您的那样,在这种情况下,您不包含任何名称字段。所以,而不是这样做:

out.println("<td>" + "<input type=\"radio\" name=\"choise\" value=\"choise\" /> </br>" + "</td>");

你可以这样做:

out.println("<td>" + "<input type=\"radio\" name=\"name\" value=\"choise\" /> </br>" + "</td>");

而且,没有表单标签,也没有提交按钮,那么,如果你不发送数据,你如何假装发送选定的学生?我认为您没有考虑过这一点,因为您真的不了解应用程序正在运行。所以简短的回答是这样做:

out.println("<form method=\"get\" action=\"/datainsert/DataInsertTable\">"); //added line
out.println("<tr>");
while (rs.next())   {
  ...//you get the idea
}
out.println("</form>"); //added line

但是为了解决这类问题,您必须阅读有关 Java Web 编程良好实践的教程!

于 2013-06-03T12:00:43.477 回答
0

就在DataInsertTable这条线之后

out.println("</html>");

你必须添加

response.setAttribute("EDIT", "yes");

因为您正在根据 EDIT 参数更改目标,但您从未真正设置它。也不要这样做:

out.println("<td>" + "<input type=\"submit\" value = \"Edit\">" + "<a href='"+req.getContextPath()+"/DataUpdate'></a> " + "</input>");

你应该做

out.println("<td>" + "<input type=\"submit\" value = \"Edit\">" + "<a href='"+req.getContextPath()+"/DataUpdateTable'></a> " + "</input>");

注意:我假设您将DataUpdateTableservlet映射DataUpdateTableaction="/SimpleServlet/DataUpdateTable";. 但我只能确定您是否提供了配置。

另外,我不知道您在这里假装什么,但这没有任何意义,因为您已经处于一种形式

out.println("<td>" + "<FORM>" + "<INPUT Type=\"BUTTON\" VALUE=\"Add More Users\" ONCLICK=\"window.location.href='"+ req.getContextPath()+"/index.jsp'\">" + "<FORM>" + "<td>");
于 2013-06-13T12:11:54.013 回答