Solved

retrieving resultset of unknow column size

Posted on 2002-07-22
4
288 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 65
servlet filter example 37 59
eclipse apache tomcat admin console 52 96
hibernate example for saving data 19 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

776 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