| Author |
javax. servlet. Servlet Exception: java. sql. SQLException
|
Harshal Gurav
Ranch Hand
Joined: May 29, 2008
Posts: 150
|
|
Hi, I am stuck here about 2-hours. Kindly help me. In this my updateuser.jsp page I am retrieving the data from drop down list which is in my search user.jsp . I am taking form name of searchuser.jsp page for reference here which is form1. in my user table there is column userid, emailed, password: Now here I am trying to display the data about the user selected from drop down list in searchuser.jsp Here is an my updateuser.jsp: <body> <% String form=request.getParameter("form1"); InitialContext context = new InitialContext(); DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/mynewdatabase"); Connection conn = ds.getConnection(); context.close(); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select emailid,password from user where emailid='"+form+"'"); rs.next(); //// String EMAIL = rs.getString("emailid ");//// String PASSWORD = rs.getString("password"); %> <table border="1"> <thead> <tr> <th><b>MODIFY USER</b></th> </tr> </thead> <tbody> <tr> <td>E-MAIL</td> <td><input type="text" name="email" value="<%=rs.getString("EMAIL")%>" size="30" /> </td> </tr> <tr> <td>PASSWORD</td> <td><input type="text" name="password" value="<%=rs.getString("PASSWORD")%>" /></td> </tr> <tr> <td><input type="submit" value="SUBMIT" name="submit" /></td> <td><input type="reset" value="RESET" name="res" /></td> </tr> </tbody> </table> </body> But the error shows at marked line as: javax.servlet.ServletException: java.sql.SQLException: Illegal operation on empty result set. org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850) Whether I am trying in the right way? Any suggestion is highly appreciated Thanks and Regards Harshal [ July 10, 2008: Message edited by: Bear Bibeault ]
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Instead of blindly using the
rs.next();
you check whether the resultset is empty or not before you could retrieve values from it. It should better be
while(rs.next()) { //do whatever you want with the resultset here! }
Also try to use a meaningful subject line so as to avoid confusions and reduce the # of characters on the title of the thread!  Again, whenever you post the Java code, try to use the code tags so that the code will look neatly!
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Rajkumar balakrishnan
Ranch Hand
Joined: May 29, 2008
Posts: 445
|
|
Try Raghavan's suggestion and what is "form1". Here you must put the name of the combo box from where you fetch the data.So instead of
String xx=request.getParameter("form1");
try using
String xx=request.getParameter("combo box name");
Hope this helps and use a meaningful heading for your post..
|
Never try to be a hard-worker. Be a smart-worker.
My Blog
|
 |
Harshal Gurav
Ranch Hand
Joined: May 29, 2008
Posts: 150
|
|
Hi, Thanks a lot, I updated it with rs.close(); But now showing the Error like: java.lang.NullPointerException com.mysql.jdbc.ResultSetImpl.buildIndexMapping(ResultSetImpl.java:709) at the same line. Need Your Help. Regards Haresh
|
 |
Rajkumar balakrishnan
Ranch Hand
Joined: May 29, 2008
Posts: 445
|
|
I updated it with rs.close(); But now showing the Error like: java.lang.NullPointerException com.mysql.jdbc.ResultSetImpl.buildIndexMapping(ResultSetImpl.java:709) at the same line.
It's hard to say without relevant code. Do post your ResultSetImpl.java here or just tell us what is there?
|
 |
Harshal Gurav
Ranch Hand
Joined: May 29, 2008
Posts: 150
|
|
Hi, Here ia an searchuser.jsp page: And here is an updateuser.jsp Any more clarification Thanks harshal
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
|
Just check your resultset and try to print the results!
|
 |
Harshal Gurav
Ranch Hand
Joined: May 29, 2008
Posts: 150
|
|
Hi; Thanks for your patience can you more elaborate it? waiting your responce. Regards harshal
|
 |
Rajkumar balakrishnan
Ranch Hand
Joined: May 29, 2008
Posts: 445
|
|
Just change the
<input type="text" name="email" value="<%=rs.getString("EMAIL")%>" size="30" />
to
<input type="text" name="email" value="<%=EMAIL%>" size="30" />
and for password too.. Then to retrieve the data from DB you must have a code like this
while(rs.next()) { //retrieve data }
First took your own time to learn JDBC concepts.This link may helpful to you..Please do read that before you post your next query. This will makes you more clear with JDBC. [ July 10, 2008: Message edited by: Rajkumar balakrishnan ]
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Harshal, Looking at the error message you have pasted at first
Can you please try executing the same SQL Query in the SQL Editor or with the MySQL Client and see are there any matching rows for the username you specify? And did you change the code to first check the resultset being non-null before attempting to fetch any rows from it? You have seemed to use in your searchuser.jsp but not in updateuser.jsp. I suggested to use Try doing this way so that it will not throw any exceptions for the resultset being empty but attempted! Important! Moreover, you are closing the resultset before you try to fetch the values from it. Uncomment that statement. See your code below [ July 10, 2008: Message edited by: Raghavan Muthu ]
|
 |
Rajkumar balakrishnan
Ranch Hand
Joined: May 29, 2008
Posts: 445
|
|
First do some research on the topic before you post queries here. This will make ranchers to avoid answering for some basic questions like closing resultset before you retrieve data from it. Please do read some nice tutorials available online.This is what we expect from the person who post their queries here... And try Raghavan's suggestion....It works like a charm... Try to use MVC in future.. Using JDBC in JSP is not a good practice...
|
 |
 |
|
|
subject: javax. servlet. Servlet Exception: java. sql. SQLException
|
|
|