Arun.K
Arun.K

Reputation: 103

Display column names and records from table in a JSP Page

I know this question may be asked in many different ways, but i am kind of stuck in the code where i cannot display the column names along with the records. I am getting records displayed but not the column names. Any help appreciated, my code so far is below -

resultSet = statement.executeQuery(sql);
			ResultSetMetaData metadata = resultSet.getMetaData();
			int count = metadata.getColumnCount();
			
		while(resultSet.next())
		{
		%>
		<tr>
		<%
		ArrayList<String> columns = new ArrayList<>();
		for(int i=1; i<=count;i++)
			{ 
		     String name = metadata.getColumnName(i);
			 columns.add(name);
			  %>
				<td>
				<%= resultSet.getString(i)%>
				
                </td>
                <% 
             }
                %>                   
             </tr>
        
		<%
		}
		
		connection.close();
		} catch (Exception e) {
		e.printStackTrace();
		}
		%>
		</table>
		</body>
		</html>		

Modified code with @swati suggestion. I have added table before try and included the array and for loop in the try. This is throwing syntax and constructor error

<table style="width:100%;text-align:left;border-collapse:collapse;background-color:gold;" border="2" bordercolor="Blue">
		<tr style="background-color:yellowgreen;color:white;">
		</tr>
		<%
		try{
			Connection connection = null;
			ResultSet resultSet = null;
			int counter=1;
			Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection("jdbc:oracle:thin:@DEV:1521:DEV","DEV","DEV");
			Statement statement = connection.createStatement();
			String sql = ("SELECT * FROM tblnm");
			resultSet = statement.executeQuery(sql);
			ResultSetMetaData metadata = resultSet.getMetaData();
			int count = metadata.getColumnCount();
			ArrayList<String> columns = new ArrayList<>(); 
    <table>
    <tr> //row for displaying column names
        <%for(int i=1; i<=count;i++)
            { 
             String name = metadata.getColumnName(i);
             columns.add(name);      
        %>
          <td><%=name%></td> //displaying column name
        <% } %>
    </tr>
     <%
      while(resultSet.next())
        {
     %>
    <tr>  
        //looping through names      
         <% for(int i=1;i<columns.size();i++) { %>
          //get values from particular columns  
            <td><%= resultSet.getString(columns.get(i))%></td>
         <% } %> //closing for loop
    </tr>
       <%
        }
    		
    		connection.close();
    		} catch (Exception e) {
    		e.printStackTrace();
    		}
    		%>
    		</table>
    		</body>
    		</html>	

Upvotes: 0

Views: 1623

Answers (1)

Swati
Swati

Reputation: 28522

Your table structure is wrong .You need to display all column name first then all rows under columns currently your code doesn't show any column name because you are not writng any code to do the same.Instead your code should look like below :

resultSet = statement.executeQuery(sql);
 ResultSetMetaData metadata = resultSet.getMetaData();
 int count = metadata.getColumnCount();
 ArrayList<String> columns = new ArrayList<>(); 
    <table>
    <tr> //row for displaying column names
        <%for(int i=1; i<=count;i++)
            { 
             String name = metadata.getColumnName(i);
             columns.add(name);      
        %>
          <td><%=name%></td> //displaying column name
        <% } %>
    </tr>
     <%
      while(resultSet.next())
        {
     %>
    <tr>  
        //looping through names      
         <% for(int i=1;i<columns.size();i++) { %>
          //get values from particular columns  
            <td><%= resultSet.getString(columns.get(i))%></td>
         <% } %> //closing for loop
    </tr>
       <%
        } //closing while loop 
       //other codes put here
       %>
  </table>

Upvotes: 1

Related Questions