0

Hi guys,

I have a quick and interesting question there for you:

I have my DB and a execute a query to read some data from the DB and return it to the HTML.
The data that is returned depends on the search fields the user had filled in.
So if he had only filled lets say Experience and rating out of several possible fields, the query would produce a table containing just these two parameters.

Now, when I fetch my result, I want to read the names of the columns that the query returned, in order to write them in the html as table headings. How do I do this?


Here simple structure of what I mean:

resultSet resQuery = null;
resQuery = stmt.executeQuery(searchQuery);

 if( resQuery != null ) {
    	out.println("<TR>");
    		
    	try{
    	while(resQuery.next()) {
    		out.println("<TD>" + resQuery.[I]getNext()[/I] +"</TD>");
    	}        
       out.println("</TR>");

 // and then when the HTML table has the appropriate headings, let the values flow into the table

    	while(resQuery.next()) {
    			String resKname = null;		
    			String resTalent = null;
    			String resStyle = null;
    			String resInstrument=null;
    			int resExperience=0;
                        ..............
out.println("<TR><TD><b>"+resKname+"</TD><TD>"+resTalent+"</TD><TD>"+resStil+"</TD>......
}

This shouldn't be that difficult?

Edited by fankoff: n/a

1
Contributor
4
Replies
5
Views
7 Years
Discussion Span
Last Post by fankoff
0

I`ve just noticed, this should also regard the part where the actual values are filled in the table under the appropriate table heading.

How do I then say: "give only these values, that are in the table"

Now I`m printing all the possible values that could be contained in a resultset and when it doesnt find them, it of course crashes.

out.println("<TR><TD><b>"+resKname+"</TD><TD>"+resTalent+"</TD><TD>"+resStyle+"</TD><TD>"+resInstrument+"</TD><TD>"+resExperience+"</TD><TD>"+resRating+"</TD><TD>"+resStatus+"</TD></TR>");


Thanks a lot!

0

Ok, I found the solution for the first question:
....
out.println("<TD>" + resQuery.getMetaData().getColumnName(k) +"</TD>");

So please give me some help about the second one:
out.println("<TR><TD><b>"+resKname+"</TD><TD>"+resTalent+"</TD><TD>"+resStyle+"</TD><TD>"+resInstrument+"</TD><TD>"+resExperience+"</TD><TD>"+resRating+"</TD><TD>"+resStatus+"<.. ..
how can I make this change dynamically and to not have to print all the values (most of which would be null)?

0

So basically what I`ve ended up with is the follwing:

if( resQuery != null ) {
    	out.println("<TR>");
    		
    	try{
    		int numOfColumns = resQuery.getMetaData().getColumnCount();
    	 	
    	 	for(int k=1;k<=numOfColumns;k++){
    	 	
    		out.println("<TD>" + resQuery.getMetaData().getColumnName(k) +"</TD>");
     	}
    	 	out.println("<TR>");
    	 	
    	while(resQuery.next()){
    		for(int k=1;k<=numOfColumns;k++){
        	 	out.println("<TD><b>"+resQuery.getString(resQuery.getMetaData().getColumnName(k))+"</TD>");
    			}
    	 	}
    	
    		out.println("</TR>");
    	}catch (Exception e) {e.printStackTrace(); }

Is this logically ok? It works, but it doesn`t display the result properly. Where could be the problem?

0

Ok, I got it now!

if( resQuery != null ) {
    	out.println("<TR>");
 
    	try{
    		int numOfColumns = resQuery.getMetaData().getColumnCount();
 
    	 	for(int k=1;k<=numOfColumns;k++){
 
    		out.println("<TD>" + resQuery.getMetaData().getColumnName(k) +"</TD>");
     	}
    	 	out.println("<TR>");
 
    	while(resQuery.next()){
    		for(int k=1;k<=numOfColumns;k++){
        	 	out.println("<TD><b>"+resQuery.getString(resQuery.getMetaData().getColumnName(k))+"</TD>");
    			}
                [B][I] out.println("</TR>");[/I][/B]
    	 	}
 
    		
    	}catch (Exception e) {e.printStackTrace(); }

I`m sorry for the thread, but I`m sure this would be useful for other novice as me ;)

cheers

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.