如何在jsp页面中动态创建表和行的列 [英] How to dynamically create row and column of table in jsp page

查看:763
本文介绍了如何在jsp页面中动态创建表和行的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库检索数据到表。
但是数据必须动态加载。

I am trying to retrieve data from database into table. But data must be loaded dynamically.

如何动态创建行和列我不知道?
如果它将只创建行,那么我会做它很容易,但我还想在页面上动态创建列,这就是为什么我困惑如何执行它

How to dynamically create row and column I don't know? If it will be only row to create then I will do it easily but I also want to create column dynamically on page so that's why I am confused how to perform it?

我的JSP代码:

<table width="59%" border="1">
    <%
        MySql1 o = new MySql1();
        o.connect();
        ResultSet r;
        int counter=1;
        String q = "select * from category_master;";
        r = o.getdata(q);
        while(r.next())
        {
            %>
                <tr>
                     <td><%= r.getString(1)%></td>                                      
                </tr>
            <% 
        }
    %>
</table>

现在我显示< td> 但是如果用户不知道要检索多少列,那么该怎么办?
select查询我使用了 * ,所以我很困惑的采取 ; td>
我想所有的动态,因为我将传递表名称也动态使用任何文本框或url。

Right now I am displaying first column in <td> but if the user don't know how many columns are going to be retrieved then what to do ? In select query I have used * so I am confused for taking <td>. I want to all dynamic because suppose I will pass table name also dynamically using any textbox or url.

这里 MySql 1是一个具有执行操作的方法的类文件。
connect()用于与db连接, getdata()用于检索数据作为参数传递的查询和 getdata()方法的返回类型为 Resultset

Here MySql1 is one class file that has method to perform operation. connect() is used to connect with db, and getdata() is used to retrieve data of query passed as argument and return type of getdata() method is Resultset.

这就是为什么我想要所有动态,但我不知道如何做。

So that's why I want all dynamic, but I don't know how to do that.

推荐答案

请尝试此代码:

<table width="59%" border="1">
    <%
        MySql1 o = new MySql1();
        o.connect();
        ResultSet r;
        int counter=1;
        String q = "select * from category_master;";
        r = o.getdata(q);
        ResultSetMetaData metaData = r.getMetaData();
        while(r.next())
        {
            %>
                <tr>
                 <%
                 for(int i = 1; i<=metaData.getColumnCount();i++)
                    { %>
                     <td>
                     <%= r.getString(i)%>
                     </td>
                <% 
                    }
                %>                   
                </tr>
            <% 
        }
    %>
</table>

这篇关于如何在jsp页面中动态创建表和行的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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