How to fetch data from database and display in html table using java

Hi,
I have mysql table datas(id int, fname varchar(20)). I want to fetch the rows in the database and display them in the html table. If I have 10 rows,

1 Lion
2 Tiger
3 Snake
4 Cheetah
5 Snake
6 Monkey
7 Elephant
8 Zebra
9 Giraffe
10 Kangaroo

the HTML page should create a table with 3 columns and 4 rows. In the first column of HTML table,

1 Lion
2 Tiger
3 Snake

should be displayed. In the second column,

4 Cheetah
5 Snake
6 Monkey

should be displayed
in the third column,

7 Elephant
8 Zebra
9 Giraffe
10 Kangaroo 

should be displayed.
If I have 20 columns, columns are still 3, but the rows increase.

How can I do that?
I have the following code

<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");%>
<% ResultSet rs = null; %>
<HTML>
<HEAD>
      <TITLE>The employees Database Table </TITLE>
</HEAD>
<BODY>
<H1>The employees Database Table </H1>
    <%
      Connection connection = null;
      try
      {
         String url = "jdbc:mysql://localhost:3306/test";
         String user = "root";
         String password = "root";
         connection = DriverManager.getConnection(url, user, password);
         Statement statement = connection.createStatement() ;
         String sql = "select * from employees order by first";
         rs = statement.executeQuery(sql);
     }
     catch(SQLException e)
     {
         System.out.println("Error!!!!!!" + e);
     }
    %>
    <TABLE BORDER="1">
        <TR>
           <TH>First</TH>
        </TR>
        <% while(rs.next()){ %>
        <TR>
           <TD> <%= rs.getString(3) %></TD>
        </TR>
        <% } %>
     </TABLE>
    </BODY>
</HTML>

Updated 27-Jun-13 20:50pm


try this..

<table border="1">
        <tr>
           <th>First</th>
        </tr>
        <% int i=0;
while(rs.next()){ %>
        <tr>
           <td> <%= rs.getString(3) %></td>
            <%if{(i%3)%>
           <tr><![CDATA[<%}%>]]>
           <% i++; } %>
        </tr> 
        
     </tr></table>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AllEmployeesServlet extends HttpServlet {
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>All Employees</title></head>");
    out.println("<body>");
    out.println("<center><h2>All Employees</h2>");
    Connection conn = null;
    Statement stmt = null;
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      conn = DriverManager.getConnection("jdbc:odbc:Employees");
      stmt = conn.createStatement();
      String orderBy = request.getParameter("sort");
      if ((orderBy == null) || orderBy.equals("")) {
        orderBy = "SSN";
      }
      String orderByDir = request.getParameter("sortdir");
      if ((orderByDir == null) || orderByDir.equals("")) {
        orderByDir = "asc";
      }
      String query = "SELECT Employees.SSN, Employees.Name, " + "Employees.Salary, "
          + "Employees.Hiredate, Location.Location " + "FROM Location " + "INNER JOIN Employees "
          + "ON Location.Loc_Id = Employees.Loc_Id " + "ORDER BY " + orderBy + " " + orderByDir
          + ";";
      ResultSet rs = stmt.executeQuery(query);
      while (rs.next()) {
        long employeeSSN = rs.getLong("SSN");
        String employeeName = rs.getString("Name");
        long employeeSalary = rs.getLong("Salary");
        Date employeeHiredate = rs.getDate("Hiredate");
        String employeeLocation = rs.getString("Location");
        out.print(employeeSSN + "::");
        out.print(employeeName + "::");
        out.print(employeeSalary + "::");
        out.print(employeeHiredate + "::");
        out.print(employeeLocation + "::");
      }
    } catch (SQLException e) {
      out.println("An error occured while retrieving " + "all employees: " 
          + e.toString());
    } catch (ClassNotFoundException e) {
      throw (new ServletException(e.toString()));
    } finally {
      try {
        if (stmt != null) {
          stmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException ex) {
      }
    }
    out.println("</center>");
    out.println("</body>");
    out.println("</html>");
    out.close();
  }
}
25.33.Database
25.33.1. Servlet Database Connection
How to fetch data from database and display in html table using java
25.33.2. Servlet Update Database
How to fetch data from database and display in html table using java
25.33.3. Servlet Database ResultSet Display Helper
How to fetch data from database and display in html table using java
25.33.4. Servlet Database Gif Decoder
How to fetch data from database and display in html table using java
25.33.5. Returns the list of the most popular flavors
25.33.6. Read data from Database and display it in a HTML table

How can we fetch data from database in Java and display in HTML?

Program to display data from database through servlet and JDBC.
import java.io.*;.
import javax.servlet.*;.
import javax.servlet.http.*;.
import java.sql.*;.
public class display extends HttpServlet..
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException..

How fetch data from SQL and display table in HTML?

Steps to Display Data From MySQL Database with PHP.
Connect PHP to MySQL Database. You can use the following database connection query to connect PHP to the MySQL database. ... .
Insert Data Into PHPMyAdmin Table. ... .
Fetch Data From MySQL Table. ... .
Display Data in HTML Table. ... .
Test Yourself to insert data..

How show data from database table in HTML?

When you display table data in a web browser from a MySQL database using a PHP application, it will be displayed unformatted. echo "</table>"; mysql_close($con);

How fetch data from database to HTML?

Use the following steps for fetch/retrieve data from database in php and display in html table:.
Step 1 – Start Apache Web Server..
Step 2 – Create PHP Project..
Step 3 – Execute SQL query to Create Table..
Step 4 – Create phpmyadmin MySQL Database Connection File..
Step 5 – Create Fetch Data PHP File From Database..