0

I have got a jsp page which has 5 columns and 12 rows .I have to retrieve data in such a way that the first record fetched should go in to first row,,,,the second in second row.....How can I do it?

RAS
  • 8,100
  • 16
  • 64
  • 86
PROXY
  • 619
  • 1
  • 5
  • 4
  • 1
    possible duplicate of [Displaying database result in JSP](http://stackoverflow.com/questions/4196197/displaying-database-result-in-jsp) – BalusC Mar 01 '11 at 12:36

3 Answers3

1

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

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
1

Start from Basic JSP Sample : Database Access - JDBC.

zengr
  • 38,346
  • 37
  • 130
  • 192
asgs
  • 3,928
  • 6
  • 39
  • 54
0

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>
Shay Rojansky
  • 15,357
  • 2
  • 40
  • 69