Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
VB
1 Lion
2 Tiger
3 Snake

should be displayed. In the second column,
VB
4 Cheetah
5 Snake
6 Monkey

should be displayed
in the third column,
VB
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
XML
<%@ 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>
Posted
Updated 27-Jun-13 20:50pm
v5
Comments
Victor Rene 26-Jun-13 6:58am    
What is the scripting language of your server ? pHp, Python, Ruby, Perl ? Anything can work but it's not really possible to paste code if we don't know.
AnushaMallajo 26-Jun-13 7:09am    
Can we do it in Java or Javascript? I don't know any of the languages you asked
TorstenH. 26-Jun-13 8:48am    
I added a homework tag - cause that's what this is.
Please do it yourself and ask specific when you've got problems.
srinvias kumar lumeris 27-Jun-13 0:41am    
instead of exporting to single file, export the whole data to multiple files (4 or 5)
AnushaMallajo 28-Jun-13 2:51am    
@srinivas kumar lumeris cannot do that. Peformance issues

1 solution

try this..

Java
<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>
 
Share this answer
 

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