Solved

retrieving resultset of unknow column size

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert from xls to xlsx using java 7 54
Bot application - advice 3 37
Running JavaFX on the Raspberry Pi 27 37
learn programming 8 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 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