-2

我已经准备了一个用于插入、查看和删除数据的表单,现在我想添加选项来编辑该数据,但要么我按照错误的方式进行操作,要么我不知道该怎么做。

那么任何人都可以帮助我继续编辑输入的数据吗?我应该参考旧的html页面来编辑还是新页面或.. ?? 我知道我没有遵循 MVC 结构,但我想在单页中完成整个编码,所以等待帮助.. 谢谢。

这些是我的代码。

数据存储.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>

数据插入表.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();
            }
    }
4

1 回答 1

0

首先在页面 HTML 页面上创建一个隐藏字段以识别您最初要执行的操作,其值应为空白,并根据单击插入更新或删除为所有可用操作分配一些唯一值。并从您的 servlet 获取该字段作为req.getParameter("<HIDDEN FIELD NAME>")并基于此,将您的代码放入相应的 if 和 else if 块中,以便仅运行该代码。希望这会有所帮助

于 2013-06-10T07:08:22.853 回答