5

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?

My JSP code :

<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>

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.

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63

2 Answers2

10

try this code:

<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>
Gaurav Singla
  • 1,405
  • 1
  • 17
  • 17
  • Sir is it possible that if i will write query to fetch all data using select * but i don't want one field. such thing possible or not ? suppose it is table of user detail and i don't want to display password except it i want all data. – Java Curious ღ Sep 30 '13 at 08:43
  • look at this question:http://stackoverflow.com/questions/14253994/selecting-all-fields-except-only-one-field-in-mysql – Gaurav Singla Sep 30 '13 at 08:46
  • Sir i am not getting it exactly what it means and i am beginner in this field so that's why i am facing problem. will you tell me how to perform it ? – Java Curious ღ Sep 30 '13 at 09:54
  • Sir i have implemented 2 mechanism. In first i have fetched column name from table and display as header and after that in second i have fetched data from table name but it shows an error in ResultSetMetaData declaration in second implementation, if i pass table name directly not a variable fetched from other page then it shows an error in ResultSetMetaData delaration... – Java Curious ღ Sep 30 '13 at 11:07
  • I have posted my question with code sir .http://stackoverflow.com/questions/19095898/error-shows-on-resultsetmetadata – Java Curious ღ Sep 30 '13 at 13:51
0

I did jsp page for testing like this

here it is test.jsp

<table width="59%" border="1">
    <thead>
    <tr>
        <td>Volume</td>
        <td>XmlTitle</td>
        <td>getYear</td>
    </tr>
    </thead>
    <tbody>
    <%
        Journal journal = Journal.findByCode("antipoda");
        List<Issue> normalIssues = journal.getIssuesOfType(IssueType.NORMAL);
        for (Issue issue : normalIssues) {

            out.print(String.format("<tr>" +
                                        "<td>%s</td>" +
                                        "<td>%s</td>" +
                                        "<td>%d</td>" +
                                    "</tr>",
                    issue.getVolume(),issue.getXmlTitle(),issue.getYear()));
        }

        out.flush();
    %>
    </tbody>
</table>

you can change the scriptlet to the code that iterates thru the model you want to display

hope this helps

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92