如何将动态值传递给java serlvet?

时间:2016-12-04 14:03:15

标签: java oracle servlets

(更新)

我正在使用java servlets和oracle sql来建立酒店管理 我从数据库打印一个html表,如果我更改了将它们传递给另一个servlet的值,我需要更新一行 这是我的第一个servlet

    if(request.getParameter("hotelTableBtn") != null){
        String query = "select * from Hotels";
        response.setCharacterEncoding("utf-8");
        Connection conn;

        try{
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","root");
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(query);
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();

            pw.println("<table border=1 style=border-collapse:collapse>");  
            pw.println("<tr>");

            for(int i = 1; i <= columnCount; i++){
                pw.println("<th>" + metaData.getColumnName(i));
            }

            pw.println("</tr>");
            pw.println();
            String name = "";

            while(rs.next()){                       
                int i;
                int row = rs.getRow();

                pw.write("<form action=/ThesisAdmin/PrintTables method=post target=_self>");
                pw.println("<tr>");


                for(i = 1; i <= columnCount; i++){

                    if(i == 1)
                        name = "name";
                    else if (i == 2)
                        name = "category";
                    else if (i == 3)
                        name = "address";
                    else if (i == 4)
                        name = "phone";
                    else
                        name = "email";

                    pw.println("<td style=border:none> <input type=text value=" + rs.getString(i) + " name=" + name + "> </td>");                           
                }
                    pw.println("<td> <input type=submit value=update name=updateBtn> </td>");

                    pw.println("</tr>");
                    pw.write("</form");

                    pw.println();
                }
                pw.println("</table>");

        }catch (Exception e){
            pw.write("" + e);
        }
    }

这是html表的一个示例

table sample

现在,我需要更新行值并将它们发送到数据库,这里是第二个servlet PrintTables

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html");
    Writer writer = response.getWriter();

    try{
        Class.forName("oracle.jdbc.driver.OracleDriver");
    }catch(Exception e){
        writer.write("" + e);
    }

    if(request.getParameter("updateBtn") != null){

        String  hotelName = request.getParameter("name");
        String  hotelCategory = request.getParameter("category");
        String  hotelAddress = request.getParameter("address");
        String  hotelPhone = request.getParameter("email");
        String  hotelEmail = request.getParameter("phone");


        try{
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","root");
            String update = "update Hotels set Name=?,Category=?,Address=?,Phone=?,Email=? where Name= '"+hotelName+"' ";
            PreparedStatement pst = conn.prepareStatement(update);

            pst.setString(1, hotelName);
            pst.setString(2, hotelCategory);
            pst.setString(3, hotelAddress);
            pst.setString(4, hotelPhone);
            pst.setString(5, hotelEmail);
            pst.executeUpdate();

        }catch(Exception e){
            writer.write("" + e);
        }
    }
}

我更新了我的代码,但只有第一个按钮才有效。有什么想法吗? 先感谢您。

(更新)

最后我的程序通过每行添加一个新表来工作。另外还添加了一个用于删除的按钮。解决方案:

            pw.println("<table border=1 style=border-collapse:collapse>");  

            pw.println();
            String name = "";
            int cnt = 0;    

            while(rs.next()){                       
                int i;
                //int row = rs.getRow();

                pw.println("<tr>");
                pw.println("<table><tr><td>");
                pw.println("<tr>");

                if(cnt == 0){
                    for(i = 1; i <= columnCount; i++){
                        pw.println("<th>" + metaData.getColumnName(i));
                    }
                    cnt = 1;
                }


                pw.println("</tr>");
                pw.write("<form action=/ThesisAdmin/PrintTables method=post target=_self>");

                for(i = 1; i <= columnCount; i++){

                    if(i == 1)
                        name = "name";
                    else if (i == 2)
                        name = "category";
                    else if (i == 3)
                        name = "address";
                    else if (i == 4)
                        name = "phone";
                    else
                        name = "email";

                    pw.println("<td style=border:none> <input type=text value=" + rs.getString(i) + " name=" + name + "> </td>");                           
                }

                    pw.println("<td> <input type=submit value=update name=updateBtn> </td>");
                    pw.println("<td><input type=submit value=delete name=deleteBtn> </td>");

                    pw.write("</form>");
                    pw.write("</td></tr></table>");
                    pw.println("</tr>");
                    pw.println();
                }
                pw.println("</table>");

table

1 个答案:

答案 0 :(得分:0)

数据库表和html表中的每一行代表一个对象 每个对象都必须有一个id

您应该为每行添加一个带有对象ID的隐藏输入字段 每个行/对象都有一个单独的表单。