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?

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!

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)?

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.