• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

showing results page by page  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. Once the servlet ends, the ResultSet will be gc'ed.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!