Forum Stats

  • 3,873,311 Users
  • 2,266,532 Discussions
  • 7,911,502 Comments

Discussions

java.sql.SQLException: Before start of resultset

800304
800304 Member Posts: 155
Hi,
I am following the following steps:
1. Create the jsp CustDetLcl1.jsp which get the locality input from the supervisor.
2. The CustDetLcl1.jsp transfers the control to CustDetLcl.java which pulls the records of all the customers lying in the locality and forwards the control to CustDetLcl2.jsp
3. The records are displayed on CustDetLcl2.jsp

I am getting the following error message when CustDet.Lcl.java runs:
java.sql.SQLException: Before start of resultset
It is not displaying the records of all the customers that belog to that locality. Pls help.

The code for CustDetLcl.java is as follows:
package supervisor.locality;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
import com.database.*;
import supervisor.locality.LocalityBean;
public class CustDetLcl extends HttpServlet 
{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
HttpSession session = request.getSession();
try
{
String X = request.getParameter("locality");
System.out.println(X);
Database db = new Database();
Connection con = db.useConnection();
String query = "select c.custname,c.address,c.locality,c.custid,c.amtoutstanding,c.interest,c.totalamtdue,a.fieldexecname from custinfo c, assignlcl a where c.fieldexeccode=a.fieldexeccode and c.locality=?";
PreparedStatement stat = con.prepareStatement(query);
System.out.println(stat);
stat.setString(1,X);
ResultSet rs = stat.executeQuery();
int count = 0;
String A[] = new String[1000];
String B[] = new String[1000];
String C[] = new String[1000];
String D[] = new String[1000];
String E[] = new String[1000];
String F[] = new String[1000];
String G[] = new String[1000];
String H[] = new String[1000];
for(int i=0;i<1000;i++)
{
A[i] = rs.getString("custname");
B[i] = rs.getString("address");
C[i] = rs.getString("locality");
D[i] = rs.getString("custid");
E[i] = rs.getString("amtoutstanding");
F[i] = rs.getString("interest");
G[i] = rs.getString( "totalamtdue");
H[i] = rs.getString("fieldexecname");
		
LocalityBean cb = (LocalityBean)session.getAttribute("cb");
if(cb==null)
{
cb = new LocalityBean(A<em>, B[i], C[i], D[i], E[i], F[i], G[i],H[i]);<br />
session.setAttribute("cb", cb);<br />
}<br />
System.out.println("custname" + A + "address" + B + "locality" + C + "custid" + D + "amtoutstanding" + E + "interest" + F + "totalamtdue" + G + "fieldexecname" + null );<br />
RequestDispatcher dis = request.getRequestDispatcher("/CustDetLcl2.jsp");<br />
System.out.println("Dispatched" + dis);<br />
dis.forward(request, response);<br />
}<br />
}<br />
catch(Exception e)<br />
{<br />
System.out.println(e);<br />
e.printStackTrace();<br />
}<br />
}<br />
}<br />
	
<pre class="jive-pre"><code class="jive-code">                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

Comments

  • 800345
    800345 Member Posts: 4,981
    There are a lot of problems with your code. You should read http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html

    To specifically answer your question. When a result set is generated the cursor points before the first row of results. The method rs.next() should be used to advance to the first row, then to advance to each consecutive row in the result set until next() returns null. It usually occurs in a while loop:
    while(rs.next()) {
      // get stuff from result set and put it into a collection or display ...
    }
  • 843840
    843840 Member Posts: 49,995 Green Ribbon
    Did it came to your mind to Google on the exact exception message?

    [http://google.com/search?q=java.sql.SQLException:+Before+start+of+result+set]
  • 800304
    800304 Member Posts: 155
    Hi^^,
    In this program I need to get the length of the result set. I dont think that there is any formula for getting the length. will you be able to help me.
    Actually I am trying to iterate through the result set and trying to store the values in arrays. Then I would be populating the bean with the arrays and forwarding to the next jsp where i would be printing the details.
    Thanks in advance.
  • 800559
    800559 Member Posts: 465
    If you want the count, you will have to do a
    select count(*) from .... where.... 
    and get the record count from the result set.
    You can iterate as suggested earlier using the while loop and populate a custom object and add it to a ArrayList. If you wanted to get the count to initialize the array, I would suggest to use a List and then convert to an Array using the toArray api.
  • 800345
    800345 Member Posts: 4,981
    Or, you can do it in the same DB query, without changing your syntax, and using better Java... here is how:

    1) Instead of putting the results into an Array, whose size you must know, put the results into a list, whose size is not known.
    2) Use th list's size() method to get the count
    3) If you really need an array at the end, then use List.toArray()
     //assuming a JavaBean named Customer with appropriate methods...
    List<Customer> customerList = new ArrayList<Customer>();
    while(rs.next()) {
      Customer newCustomer = new Customer();
        newCustomer.setName(rs.getString("custname"));
        newCustomer.setAddress(rs.getString("address"));
        newCustomer.setLocality(rs.getString("locality"));
        newCustomer.setID(rs.getString("custid"));
        newCustomer.setOutstandingBalance(rs.getString("amtoutstanding"));
        newCustomer.setInterest(rs.getString("interest"));
        newCustomer.setTotalDue(rs.getString( "totalamtdue"));
        newCustomer.setFieldExec(rs.getString("fieldexecname"));
    
      customerList.add(newCustomer);
    }
    
    int noOfCustomers = customerList.size();
    Customer[] customerArray = customerList.toArray(new Customer[0]);
This discussion has been closed.