Solved

retrieving resultset of unknow column size

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

757 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

20 Experts available now in Live!

Get 1:1 Help Now