Solved

retrieving resultset of unknow column size

Posted on 2002-07-22
4
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 49
Java: The Public Class Main 4 66
Eclipse for Java EE development 2 68
Is there a Google Voice API? 9 74
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

738 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