【问题标题】:How to send the SQL query results to a jsp page?如何将 SQL 查询结果发送到 jsp 页面?
【发布时间】:2009-03-13 13:07:18
【问题描述】:

我有一个包含 id(number)、name(string)、address(string) 字段的数据库。

我编写了一个 java 程序 EmployeeDAO 来执行查询。我将它存储在 ResultSet 对象rs 中。我需要将此结果集显示为 JSP 页面中的表格。如何将此rs 发送到 JSP 页面?

public class EmployeeDAO 
{
    public _____ list() throws Exception
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            String url = "jdbc:odbc:employee_dsn";
            Connection con = DriverManager.getConnection(url);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("Select * from emp_table");
        }
            catch (Exception e)
        {   
             System.out.println(e);
        }
    }
}

【问题讨论】:

    标签: jsp jdbc


    【解决方案1】:

    首先创建具有与 emp_table 中的列相同的字段的 Java 模型类 Employee。例如:

    public class Employee {
      private String name;
      private String lastName;
      public void setName(String name) {
        this.name = name;
      }
      public String getName() {
        return this.name;
      }
      public String getLastName() {
        return this.lastName;
      }
      public void setLastName(String lastName) {
        this.lastName = lastName;
      }
    }
    

    然后在你的方法 _list() 中像这样迭代结果集:

    public List<Employee> _ list() throws Exception {
         Connection con = null;
         ResultSet rs = null;
         List<Employee> result = new ArrayList<Employee>();
         try
           {
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
             String url = "jdbc:odbc:employee_dsn";
             con = DriverManager.getConnection(url);
             Statement stmt = con.createStatement();
             rs = stmt.executeQuery("Select * from emp_table");
             while (rs.next()) {
               Employee emp = new Employee();
              emp.setName(rs.getString("emp_name"));
              emp.setLastName(rs.getString("emp_last_name"));
              result.add(emp);
             }
    
            }
            catch (Exception e)
            {       
                System.out.println(e);
            } finally {
                if (null != rs) {
                  try { rs.close()} catch(Exception ex) {};
                }
                if (null != con) {
                  try { con.close()} catch(Exception ex) {};
                }
            }
    return result;
      }
    

    在您的 JSP 中,您可以像这样遍历集合:

    <table>
      <c:forEach var="emp" items="${empDao._list}">
        <tr>
          <td>${emp.name}</td>
          <td>${emp.lastName}</td>
        </tr>
      </c:forEach>
    </table>
    

    【讨论】:

      【解决方案2】:

      优雅的解决方案是将您的结果集映射到对象列表。查看 springs RowMapper 以了解如何处理此问题。

      在您的 jsp 中,您可以使用 &lt;c:forEach/&gt; 循环来写出此列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2015-01-20
        • 2014-06-08
        • 1970-01-01
        相关资源
        最近更新 更多