aspose file tools*
The moose likes JDBC and the fly likes showing results page by page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "showing results page by page" Watch "showing results page by page" New topic
Author

showing results page by page

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
The following program displays 5 rows from db table; my idea is to reinvoke
the same
servlet (when the user clicks a link to the same servlet) and display the
next 5 rows.
I would like to know that how can i prevent the resultset pointer from being
initialized
to zero when the servlet is invoked again.
< !--<br /> import javax.servlet.*;<br /> import javax.servlet.http.*;<br /> import java.io.*;<br /> import java.sql.*;<br /> <br /> public class JdbcServlet1 extends HttpServlet<br /> {<br /> ResultSet customers;<br /> file://database connection is shared by all requests<br /> static Connection dbConn;<br /> /**<br /> * init method is called when servlet is initialized.<br /> * Establishes a database connection when servlet is initially<br /> * loaded that can be shared across all requests.<br /> */<br /> public void init(ServletConfig config) throws<br /> ServletException<br /> {<br /> super.init(config); file://pass ServletConfig to parent<br /> try<br /> {<br /> file://load JDBC-ODBC Bridge driver<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> file://establish database connection<br /> dbConn = DriverManager.getConnection(<br /> "jdbcdbc:librec");<br /> }<br /> catch (ClassNotFoundException e) {<br /> System.out.println("JDBC-ODBC bridge not found!");<br /> return;<br /> }<br /> catch (SQLException e) file://DriverManager.getConnection throws<br /> {<br /> System.out.println("SQL exception thrown in init!");<br /> return;<br /> }<br /> }<br /> <br /> /**<br /> * doGet() called in response to a GET request<br /> */<br /> public void doGet(HttpServletRequest request,<br /> HttpServletResponse response) throws ServletException,<br /> IOException<br /> {<br /> try<br /> {<br /> response.setContentType("text/html"); file://returns HTML<br /> file://get handle to output stream<br /> PrintWriter out = response.getWriter();<br /> file://create statement<br /> Statement stat = dbConn.createStatement();<br /> file://query database for result set<br /> customers = stat.executeQuery(<br /> "SELECT * FROM book");<br /> file://generate HTML document to return to client<br /> out.println("<HTML>");<br /> out.println("<HEAD><TITLE>Books List</TITLE></HEAD>");<br /> out.println("<BODY>");<br /> out.println("<H2>XXX-wow</H2>");<br /> out.println("<TABLE BORDER=1>"); file://create an HTML table out.println("<TR><TH>Book code</TH>"); out.println("<TH>title</TH>"); out.println("<TH>section</TH></TR>"); int counter=0; while (customers.next()) file://iterate through all records { counter++; file://add a table row for each record out.println("<TR><TD>" + customers.getString("bookcode") + "</TD><TD>" + customers.getString("title") + "</TD><TD>" + customers.getString("section") + "</TD></TR>"); if(counter==5) break; } out.println("</TABLE>");<br /> out.println("<br /> <a<br /> href=\"http://localhost:8080/servlet/JdbcServlet12\">nexte</a>");<br /> out.println("</BODY></HTML>");<br /> out.close();<br /> }<br /> catch (Exception e)<br /> {<br /> e.printStackTrace();<br /> }<br /> }<br /> /**<br /> * Tells the server about this servlet<br /> */<br /> public String getServletInfo()<br /> {<br /> return "Sample JDBC servlet";<br /> }<br /> }<br /> <br /> -->
Thanks in advance
Dinesh
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You can't. Once the servlet ends, the ResultSet will be gc'ed.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Thomas,
A related question came up in Servet/JSP form here http://www.javaranch.com/ubb/Forum7/HTML/001335.html
Please see. I gave some thought. Yet to get input from others. I really appreciate your suggestions.
regds
maha anna
[This message has been edited by maha anna (edited December 14, 2000).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: showing results page by page