在JSP中以表格格式发布基于servlet的数据库查询结果

问题描述 投票:0回答:1

我需要在JSP中以表格格式显示数据库查询结果。我已经尝试使用forEach的各种选项,但无法实现。你能帮忙从do方法中获取数据库结果的JSP吗?

这是我的Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);

    Connection con = null;
    Statement sta = null;
    String url = "jdbc:mysql://localhost/cass_db";
    String dbdriver = "com.mysql.jdbc.Driver";
    String username = "app_user";
    String password = "passwordxxxx";

    try {
        Class.forName(dbdriver);
        con = DriverManager.getConnection(url, username, password);
        sta = con.createStatement();
    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String Sql = "Select * from doctors";
    ArrayList<String> list = new ArrayList<String>();
    String FileName = "/Users/asia/Downloads/test.log";
    FileOutputStream file = new FileOutputStream(FileName);
    try {
        ResultSet rs = sta.executeQuery(Sql);
        while (rs.next()) {
            file.write(rs.getBytes(5));
            file.write(System.getProperty("line.separator").getBytes());
            list.add(rs.getString("doctor_nric"));
            list.add(rs.getString("doctor_full_name"));
            request.setAttribute("results", list);
        }
        con.close();
        file.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    request.getRequestDispatcher("./DBResults.jsp").forward(request, response);

}

}

数据库表结构:

doctor_id, doctor_full_name, doctor_login_name, doctor_password, doctor_nric;

1, 'Daniel Ricciardo', 'dany', 'NZQbTKL2Z8pakBcUME3y/A==', 'G6345688X'

JSP代码在这里

<form action="./DBResults" name="form_DBResults" method="post">
  <label> Click here</label> <input type="submit" value="GetDBResults" id="submitBtn" />
</form>

<table border="1"> 
  <c:forEach items="${results}" var="result">
    <tr>
      <td> <c:out value="${result.doctor_nric}" /></td>
      <td> <c:out value="${result.doctor_full_name}" /> </td>
    </tr>
  </c:forEach> 
</table>

上面的jsp没有以表格格式显示结果,我得到HTTP状态500

内部服务器错误@ "<td> <c:out value="${result.doctor_nric}" /></td>"

如果我使用<td> <c:out value="${result}.{doctor_nric}" /></td>然后我得到像G6345688X.{doctor_nric}的结果,但我期待表格格式的数据库变量结果。

你能帮忙在JSP中设置DB变量及其等价物吗?

database jsp servlets
1个回答
0
投票

你可以使用jsp的显示表。没有必要使用任何。

<display:table name="someList" id="row" requestURI="MyAction.do">      
    <display:column property="id" title="identifier"/>  
    <display:column property="description" title="Comments"/>
</display:table>
© www.soinside.com 2019 - 2024. All rights reserved.