在 JSP 页面中显示来自 table 的列名称和记录

Display column names and records from table in a JSP Page

我知道这个问题可能会以多种不同的方式提出,但我有点卡在无法显示列名和记录的代码中。我正在显示记录但不是列名。任何帮助表示赞赏,到目前为止我的代码如下 -

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>  

根据@swati 建议修改代码。我在 try 之前添加了 table 并在 try 中包含了数组和 for 循环。这是抛出语法和构造函数错误

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

您的 table 结构错误。您需要先显示所有列名,然后显示列下的所有行,目前您的代码不显示任何列名,因为您没有编写任何代码来执行 same.Instead 您的代码应如下所示:

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>