aspose file tools*
The moose likes Servlets and the fly likes java.sql.SQLException: Before start of result set?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.sql.SQLException: Before start of result set?!" Watch "java.sql.SQLException: Before start of result set?!" New topic
Author

java.sql.SQLException: Before start of result set?!

Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
hi everyone,
When I post a request use a jsp and Tomcat says:"java.sql.SQLException: Before start of result set"?!Why?Mycode is:
/////////////////registe.java///////////////////
public class registe extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try{
PrintWriter out=response.getWriter();
response.setContentType("html/text");
out.println("<html>");
out.println("<body>");
out.println("Please Registe");
Properties prop=new Properties();
prop.put("user","lyo");
prop.put("password","qijiashe");
this.executequery(request,response,"org.gjt.mm.mysql.Driver","jdbc:mysql://localhost:3306/test",prop);
out.println("</body>");
out.println("</html>");

}catch(Exception e){
PrintWriter out=response.getWriter();
out.println(e.toString());
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response,Properties prop)throws ServletException,IOException{
try{
this.executequery(request,response,"org.gjt.mm.mysql.Driver","jdbc:mysql://localhost:3306/test",prop);
}catch(Exception e){
PrintWriter out=response.getWriter();
out.print(e.toString());
}
}
public void executequery(HttpServletRequest request,HttpServletResponse response,String uri,String con,Properties prop)throws Exception{
ResultSet rs;
Statement stm;
Connection conn;
PrintWriter out=response.getWriter();
String iname=request.getParameter("iname");
String ipassword=request.getParameter("ipassword");
String sql="select * from registe";
Class.forName(uri);
conn=DriverManager.getConnection(con,"lyo","qijiashe");
stm=conn.createStatement();
rs=stm.executeQuery(sql);
String sname=rs.getString("name");
String spass=rs.getString("pass");
String ssex=rs.getString("sex");
String sage=rs.getString("age");
String semail=rs.getString("email");
String stime=rs.getString("time");
if(sname.equals(iname)&&spass.equals(ipassword)){
out.println("Congratulations,you registe successfully!");
}
}
}
Can anyone can help me?Thank you very much!


When I face C# and Java I choose Java.
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
Hi
After this statement
rs=stm.executeQuery(sql);
just add
if(rs.next()) or while(rs.next()) depending upon the requirement.
I think it should work now.
Regards,
Sanjeev
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
The ResultSet class mantains a cursor pointing to the current row.
That cursor is initiallly pointing before the first row.
So before you can retrieve data from your resultset you need to call .next() in order to point to the first row.
HTH
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Hi Juanjo and Sanjeev:
Thank you very much!It can works well!You are right!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.sql.SQLException: Before start of result set?!