简体   繁体   中英

how to retrieve data from a db in jsp page

我有一个jsp页面有5列12行。我必须以这样的方式检索数据:第一行记录应该进入第一行,,,第二行第二行......怎么能我做到了吗?

connect to DB in servlet fetch the data using JDBC and set required data to request/session/application scope as needed and forward the request to view (jsp)

Also See

Completely agree with the above - in any serious production application database should happen in Java/JDBC in a proper controller, and not in the view (JSP).

But, sometimes it makes sense to use JSTL's SQL capabilities, check out a good JSTL primer here: http://www.ibm.com/developerworks/java/library/j-jstl0520/index.html

Some relevant code:

<sql:setDataSource var="dataSrc"
    url="jdbc:mysql:///taglib" driver="org.gjt.mm.mysql.Driver"
    user="admin" password="secret"/>
    <sql:query var="queryResults" dataSource="${dataSrc}">
  select * from blog group by created desc limit ?
  <sql:param value="${6}"/></sql:query>

<table border="1">
  <tr>
    <th>ID</th>
    <th>Created</th>
    <th>Title</th>
    <th>Author</th>
  </tr>
<c:forEach var="row" items="${queryResults.rows}">
  <tr>
    <td><c:out value="${row.id}"/></td>
    <td><c:out value="${row.created}"/></td>
    <td><c:out value="${row.title}"/></td>
    <td><c:out value="${row.author}"/></td>
  </tr>
</c:forEach>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM