This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sanjeev Kaushik
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Juanjo and Sanjeev:
Thank you very much!It can works well!You are right!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic