Solved

retrieving resultset of unknow column size

Posted on 2002-07-22
4
286 Views
Last Modified: 2010-03-31
Hi,

i'm creating a web page which displays results for a given query.  The thing is that the query will be entered in a free form field so I have no knowledge how many columns are going to be retrieved from the query.  

i.e.
i wouldn't now if the query is:

select name, last_name from user_table  ( returns two columns)
or
select name from user_table (returns on column)

The question is: how do I retrieve the column names of the resultset? or maybe just how many columns there are.

I basically need to be able to retrieve the data using say rs.getObject("someName"); and display them.

thanks,
- tekel
0
Comment
Question by:tekel
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 7169741
you can use ResultSetMetaData:

rsmd = rs.getMetaData();

for (int i=0;i<rsmd.getColumnCount;i++) {
   System.out.println(rsmd.getColumnname(i));
}
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7169918
Hi,

Does this piece of code helps?

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        openPage(writer);
        topHeader(writer);
        Connection connection = null;
        try{
            connection = openConnection();
            Statement invoker = connection.createStatement();
            ResultSet allData = invoker.executeQuery("select * from element");
            ResultSetMetaData cols = allData.getMetaData();
            writer.println("<table>");
            int total = cols.getColumnCount();
            writer.println("<tr>");
            for( int i = 1; i<=total; ++i){
                writer.print("<th>"+cols.getColumnLabel(i)+"</th>");
            }
            writer.println("</tr>");
            writer.println("<tr>");
            while( allData.next() ){
                for(int i=1; i<=total; ++i){
                    writer.print("<td>"+allData.getObject(i)+"</td>");
                }
            }
            writer.println("</tr>");
            writer.println("</table>");
        }
        catch(Exception err){
            writer.println("<pre>");
            err.printStackTrace(writer);
            writer.println("</pre>");
        }
        finally{
            if( connection != null ){
                try{ connection.close(); }catch(SQLException ignore){}
            }
        }
        closePage(writer);
    }

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
0
 

Author Comment

by:tekel
ID: 7169980
thanks a lot, bobbit31
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7169984
no prob :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now