1

(Updated)

I am using java servlets and oracle sql to build a Hotel management.
I print an html table from database and I need to update a row if I change values passing them to another servlet.
Here is my first 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);
        }
    }

and this is a sample of the html table

table sample

Now, I need to update row values and send them to the database, here is the second 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);
        }
    }
}

I updated my code like this but only the first button works.Any ideas? Thank you in advance.

(Update)

Finally my program worked by adding one new table per row.Also Ι added one more button for deletion.Solution:

            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

Αntonis Papadakis
  • 1,210
  • 1
  • 12
  • 22
  • You forgot the actual question. "I need to" is not a question. – Kayaman Dec 04 '16 at 14:10
  • Post edited, thanks.. – Αntonis Papadakis Dec 04 '16 at 14:21
  • 1
    Please can you explain in more detail what do you mean by "dynamic row values"? Maybe an example would help. – vanje Dec 04 '16 at 15:22
  • Maybe dynamic is not the right word, but I need to update table values and send them to the database – Αntonis Papadakis Dec 04 '16 at 16:18
  • But this doesn't help to understand what you mean. Please try to explain why you are not able to use the normal GET/POST parameters to transfer your data. Sorry, but I really don't get it where your problem is. Give an example and elaborate your difficulties to pass the data. Maybe you haven't understand your own problem fully, then it could help trying to explain it to a rubber duck. – vanje Dec 04 '16 at 16:41

1 Answers1

1

Each row in your database table and html table represent one object.
Each object must have an id.

You should add a hidden input field with the object id to each row.
And have a separate form for each row/object.

code_angel
  • 1,537
  • 1
  • 11
  • 21