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>