编辑/更新存储在数据库中的输入数据 - servlet

时间:2013-06-10 04:25:34

标签: java database servlets database-connectivity

我已经创建了一个servlet文件,我已经添加了插入,删除和查看数据库中的数据。

现在我想添加插入数据的更新,但我无法完成它。因为我在单个文件中添加了整个代码,所以我不知道如何添加它。任何人都可以帮我吗? 等待回复。

        if(req.getParameter("choise")!=null)    {
            nm = req.getParameter("choise");

            idr = "UPDATE student SET rollno = ?, class = ?, mobileno = ? WHERE name ='"+nm+"'";

            try {
                st=con.createStatement();
                int rs1 = st.executeUpdate(idr);
                pwinsert.println(rs1);
            }
            catch (Exception e) {
                e.getMessage();
                System.out.println("Error " +e);
            }

            System.out.println("Data Edited...");

        }

3 个答案:

答案 0 :(得分:0)

您可以为每个CRUD操作创建不同的Servlet。使用标志变量也可以识别要遵循的CRUD操作,但这不是一个好习惯。请检查此link

答案 1 :(得分:0)

 if(req.getParameter("choise")!=null)    {
            nm = req.getParameter("choise");
            if(nm.equals("UPDATE")){ idr = "UPDATE student SET rollno = ?, class = ?, mobileno = ? WHERE name ='"+nm+"'";

            try {
                st=con.createStatement();
                int rs1 = st.executeUpdate(idr);
                pwinsert.println(rs1);
            }
            catch (Exception e) {
                e.getMessage();
                System.out.println("Error " +e);
            }

            System.out.println("Data Edited...");



}else if(nm.equals("ADD")){

idr = "<INSERT QUERY>";

            try {
                st=con.createStatement();
                int rs1 = st.executeQuery(idr);
                pwinsert.println(rs1);
            }
            catch (Exception e) {
                e.getMessage();
                System.out.println("Error " +e);
            }

            System.out.println("Data inserted...");


}


        }

答案 2 :(得分:0)

这应该做你想做的事情

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)    {
    nm = req.getParameter("choise");

    idr = "UPDATE student SET rollno = ?, class = ?, mobileno = ? WHERE name = ?";

    try {
        PreparedStatement pst=con.prepareStatement(idr);
        pst.setString(1,roll);
        pst.setString(2,class);
        pst.setString(3,mono);
        pst.setString(4,nm);

        int rs1 = st.executeUpdate(idr);
        pwinsert.println(rs1);
    } catch (Exception e) {
        e.getMessage();
        System.out.println("Error " +e);
    }

    System.out.println("Data Edited...");

}

我建议你开始将代码分解为方法。

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

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

    Connection conn = getDatabaseConnection();
    Statement sql = createSqlStatement(conn, nm ,roll, clas, mono);

    processSqlStatement(conn,sql);

    createOutput(req,res);

}

这将使您更容易维护和添加新内容。