hi every1
i m new to website development.
just learning it.
i have made a code in which i access my database(MS Access).
i m able to print the first record of the database on the page.
but i m not able to go to next record which i want to show on click of a button.
i dont want to use while(rs.next) as i dont want to show all the records at once.
the code i m writing is->

<%@ page import="java.sql.*" %>
<htmL>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dataSourceName="mydsn";
String dbURL="jdbc:odbc:" + dataSourceName;
Connection con= DriverManager.getConnection(dbURL,"","");
Statement s=con.createStatement();
s.execute("select * from web");
ResultSet rs;
rs=s.getResultSet() ;
rs.next();
%>
<body>
<script>
function getDetails()
{
rs.next()
}
</script>
<form method="post">
Website:<input type="text" value="<%=rs.getString(1) %>"><br>
Url:<input type="text" value="<%=rs.getString(2) %>"><br>
Category: <input type="text" value="<%=rs.getString(3) %>"><br>
Description: <input type="text" value="<%=rs.getString(4) %>"><br>
Search Engine-><br>
Yahoo: <input type="text" value="<%=rs.getString(5) %>"><br>
Google: <input type="text" value="<%=rs.getString(6) %>"><br>
Altavista: <input type="text" value="<%=rs.getString(7) %>"><br>
<input type="button" value="Next" onClick="getDetails()">
</form>
</body>
</html>

Dani AI

Generated

Short diagnosis: the page you posted mixes server-side JDBC with client-side JavaScript. Server code (JSP/JDBC) runs on the server and produces HTML/JS; once the browser has the page there is no live ResultSet to call from onClick. That is why rs.next() inside a client-side function never works.

A simple, robust pattern (no open ResultSet in the browser):

  • On first request, the servlet/DAO queries the ordered list of primary keys (small, just IDs) and stores that List in session plus a numeric index (0).
  • Render the record for IDs.get(index). The page contains a Next button that either submits to the servlet (or makes an AJAX call) with a command like cmd=next.
  • The servlet increments the index (bounds-check), loads the single record by ID with a PreparedStatement, and forwards the result to the JSP. No ResultSet or Connection is kept in session — only a list of IDs and a small index.

This keeps DB work out of JSP (as suggested) and avoids storing live JDBC objects in session (bad practice). ’s move toward DAO/servlet is the right direction.

Example flow (pseudocode):

/* In servlet doGet/doPost */
List<Integer> ids = session.getAttribute("webIds");
if (ids==null) { ids = dao.fetchIds(); session.setAttribute("webIds", ids); session.setAttribute("idx",0); }
int idx = (Integer)session.getAttribute("idx");
if ("next".equals(req.getParameter("cmd")) && idx < ids.size()-1) idx++;
session.setAttribute("idx", idx);
WebRecord r = dao.fetchById(ids.get(idx));
req.setAttribute("record", r);
forward to JSP to render fields

Troubleshooting/cautions: always close connections/resultsets in finally, use PreparedStatement, handle empty result sets and end-of-list (disable Next or wrap), and prefer AJAX for a smoother UI. If the dataset is huge, don’t cache all IDs in session — use paged queries or DB-specific “next” query logic instead.

Recommended Answers

All 5 Replies

Why don't you store whole databse into bean and then on button press request next set

bro thanks for ur reply.
but i dont know how to use beans.
can u show me a small code????

nice explanation on wikipedia

bellow is how do I get data from my db about events in calendar

CalendarData[] eArr = new CalendarData[exists];
try
{				
	int i=0;
	strQuery = "select event_type, ev_date, ev_time, ev_description from calendar_events where userName='" + strUser + "' and cal_id='" + calId + "'";
	rs = stmt.executeQuery( strQuery);
	while( rs.next())
	{
		eArr[i] = new CalendarData();
		eArr[i].setEventType(rs.getString("event_type") );
		eArr[i].setEventDate(rs.getString("ev_date") );
		eArr[i].setEventTime(rs.getInt("ev_time") );
		eArr[i].setEventDescription(rs.getString("ev_description") );
		i++;					
	} // end while loop
	session.setAttribute("eventArray", eArr);
	CalendarData[] test = (CalendarData[]) session.getAttribute("eventArray");

Also do not connect to DB from JSP use servlets

thanks got the soln
:)

Nice Nice! I used the Solution given by one of the members using the DAO Pattern! I agree with that having code in JSP does not solve problems quickly but rather call the methods from servlets or from separate java files. Great Thread! I also got a solution for one of my problems!


Thankz Once Again :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.