【问题标题】:How to display different database value in jsp page table side by side如何在jsp页表中并排显示不同的数据库值
【发布时间】:2013-09-13 19:07:29
【问题描述】:

我编写了一个 jdbc 代码,我从 mysql 表中提取值并显示在 jsp 页面中。值显示在此页面上,但一个低于另一个值。我需要的是不同的值应该并排显示,即一个并排显示。

这是我的jsp代码:

    <%

      try {
  Class.forName("com.mysql.jdbc.Driver").newInstance();
   String url = "jdbc:mysql://localhost:3306/grandsho_register";
   connection = DriverManager.getConnection(url, "root", "pwd");

   String sql = "select title,link,keyword,category,image,content from adminproduct       where category='Nokia'";
  st=connection.createStatement();
 ResultSet rs=st.executeQuery(sql);
  while(rs.next()) {


  %>   


   <TABLE border="0" width="900">
<tr valign="top">
<td width="300" ALIGN=left>
    <div class="item_list">
    <iframe src = '<%=rs.getString("link") %>' frameborder = 0 height=250>    </iframe><input type="hidden" name="keyword" value="<%=rs.getString("keyword") %>" />

    </div>  </td>
    <td width="300" ALIGN=left>
    <div class="item_list">
    <iframe src = "here i need different table value" frameborder = 0 height=250> </iframe><input type="hidden" name="keyword" value="<%=rs.getString("keyword") %>" />

    </div>  </td>
    </tr>
  </TABLE>


    <%              }
   } catch (Exception e) {
    e.printStackTrace();
    } finally {
  if (st != null) {
    try {
    st.close();
    } catch (SQLException e) {
    } // nothing we can do
     }
   if (connection != null) {
    try {
    connection.close();
  } catch (SQLException e) {
  } // nothing we can do
    }
   }
  %>

这里的表有两列,我希望这些列中有不同的值。谁能给我一些解决方案?

【问题讨论】:

  • 显然这些列是 TD 部分。所以你应该只在while循环中有TD(一次),而不是整个表。
  • 我当然希望您的代码仅用于演示目的。在过去 15 年左右的时间里,从 JSP 访问数据库被认为是一种不好的做法。
  • @Olaf 是的,我知道这是错误的,只是为了演示

标签: jsp jdbc html-table


【解决方案1】:

你在table标签之前做while循环,所以你定义了两个表,只需将while循环移到tr标签之前的表内

ResultSet rs=st.executeQuery(sql);
<TABLE border="0" width="900">
  <% while(rs.next()) { %>
  <tr valign="top">
    <td width="300" ALIGN=left>
      <div class="item_list">
        <iframe src = '<%=rs.getString("link") %>' frameborder = 0 height=250></iframe>
        <input type="hidden" name="keyword" value="<%=rs.getString("keyword") %>" />
      </div>
    </td>
    <td width="300" ALIGN=left>
      <div class="item_list">
        <iframe src = "here i need different table value" frameborder = 0 height=250>  
        </iframe>
        <input type="hidden" name="keyword" value="<%=rs.getString("keyword") %>" />

      </div>  
    </td>
  </tr>
  <% }
</TABLE>

【讨论】:

  • 根据您的解决方案值仍然显示一个在另一个下方(以及另一列),我需要在上述两列中连续显示这些值。
猜你喜欢
  • 2015-09-20
  • 1970-01-01
  • 2014-04-28
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 2016-09-11
相关资源
最近更新 更多