-1

I am developing my college internship project technology using j2ee with HTML. I am able to retrieve data from the database and also able to create the table which is displaying the result. The problem is that the table is showing the result first and then on the second line it is showing the headers name. Please help me out in this and also let me know, how to create a html page with servlet code which can edit the data in the database as per required by the user. A very big thanks in advance.

Here is the code.

PrintWriter out = response.getWriter();
        String sql;
        String input = request.getParameter("list");
        String txtField = request.getParameter("txtField");

        try {

            sql = String.format("select * from adddriver where (%s) = '%s'",input, txtField);
            ResultSet rs = DBConnection.executeQuery(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int colCount = metadata.getColumnCount();
            out.println("<table border=1>" + "<tr>");  
            for(int i=1;i<=colCount;i++)  
            {  
            out.println("<th>"+metadata.getColumnName(i)+"</th>");  
            }  

            out.println("</tr>");  
            /* Printing result */  

            while(rs.next())  
            {  
                 String[] rowData = new String[colCount];
                 for(int i =0; i<colCount; i++){
                    rowData[i] =  rs.getString(i+1);
                    out.println(rowData[i]);

            //out.println("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+" </td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td>"+rs.getString(5)+" </td><td>"+rs.getString(6)+" </td></tr>");  
                 }
            }  


        }
        catch(SQLException sqe){
            sqe.getStackTrace();
        }
        } 
Vaibhav Dalela
  • 87
  • 2
  • 10

1 Answers1

1

Write your while loop like this with enclosing tr td tags :

 while(rs.next())  
 {  
    out.println("<tr>"
    String[] rowData = new String[colCount];
    for(int i =0; i<colCount; i++){
       rowData[i] =  rs.getString(i+1);
       out.println("<td>"+rowData[i]+"</td>");
    }
    out.println("</tr>");
 } 
 out.println("</table>"); ///write this outside while loop
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
  • it works... Thanks.... Can you please help me in creating a edit form which can search for and edit the data into database if required by a user. – Vaibhav Dalela Mar 31 '15 at 11:49
  • Sir,can you please help me in creating a edit form which can search for and edit the data into database if required by a user. – Vaibhav Dalela Mar 31 '15 at 11:52
  • means you want to fetch the current details of the user on an editable form and after editing by the user save it back to database?? – Brijesh Bhatt Mar 31 '15 at 11:55
  • I am using servlet technology and html. – Vaibhav Dalela Mar 31 '15 at 12:00
  • i can guide you to this .. First when your html page loads you have to make an ajax request to your servlet and fetch the details of the user in a JSON Object like this... http://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject .. and for this you have use AJAX .. – Brijesh Bhatt Mar 31 '15 at 12:11