从数据库中获取行并显示在html表中 [英] Fetch rows from database and display in html table

查看:424
本文介绍了从数据库中获取行并显示在html表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有mysql表数据(id int,fname varchar(20))。我想获取数据库中的行并在html表中显示它们。如果我有10排,



1狮子

2虎

3蛇

4猎豹

5蛇

6猴子

7大象

8 Zebra

9长颈鹿

10袋鼠



HTML页面应创建一个包含3列和4行的表格。在HTML表的第一列中,

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 

<应该显示


如果我有20列,列仍然是3,但行增加。



我该怎么做?

我有以下代码


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>

推荐答案

尝试这个..



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>


这篇关于从数据库中获取行并显示在html表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆