Click here to Skip to main content
15,116,516 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 21: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
AnushaMallajo 28-Jun-13 2:52am
   
@TorstenH i have the code.. couldn't post it as I was unable to inundate it
Prasad Khandekar 28-Jun-13 3:03am
   
Hello Anusha,

Try changing your loop as shown below.

<TABLE BORDER="1">
<TR>
<TH>First</TH>
<TH>Second</TH>
<TH>First</TH>
</TR>
<TR>
<%int colCntr = 1;
boolean rowDone = false;
<% while(rs.next()) {
rowDone = (colCntr % 3) == 0)
if (rowDone) {
colCntr = 1;%>
</TR>
<TR>
<%
} else {
colCntr++;%>
<TD><%= rs.getString(3) %></TD>
<% }
}%>
</TABLE>
</BODY>
</HTML>
AnushaMallajo 28-Jun-13 4:20am
   
Hi Prasad,
Thanks for your response.I am getting an error on line while(rs.next())

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>
   

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