File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes session and ResultSet.getDouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "session and ResultSet.getDouble" Watch "session and ResultSet.getDouble" New topic
Author

session and ResultSet.getDouble

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Hello I have couple of servlets doing different actions. I also use session to setAttribute/getAttribute between jsp and servlets.

The logic is like this:
login.jsp calls Login servlet - if ok redirect to accountInfo.jsp
accountInfo.jsp allow user to deposit amount (a double) if it is 0
accountInfo.jsp calls Deposit servlet - if ok redirect to accountInfo.jsp showing the updated amount

I got the login.jsp -> Login servlet -> accountInfo.jsp working. In accountInfo.jsp I don't seem to get the updated amount to setAttribute to the session.

The logic of Deposit servlet is like this:
1) getSession()
2) update db value
3) rs the new updated value
4) session.setAttribute the rs.getDouble() -> this is the problem
5) redirect to accountInfo.jsp

Deposit servlet


accountInfo.jsp




java.sql.SQLException at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815) at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528) at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448) at com.mysql.jdbc.ResultSetImpl.getDoubleInternal(ResultSetImpl.java:2382) at com.mysql.jdbc.ResultSetImpl.getDouble(ResultSetImpl.java:2343) at com.mysql.jdbc.ResultSetImpl.getDouble(ResultSetImpl.java:2361) at trade.Deposit.doPost(Deposit.java:60) at javax.servlet.http.HttpServlet.service(HttpServlet.java:738) at javax.servlet.http.HttpServlet.service(HttpServlet.java:831) at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94) at ...



What am I doing wrong? Thanks.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Furher to above if I do while(rs.next()) then rs.getxxx() will work.

Must I do while(rs.next()) in order to use rs.getxxx()?

How to get the exact row WITHOUT looping the entire database table?

thanks
[ May 09, 2008: Message edited by: K. Tsang ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by K. Tsang:
Must I do while(rs.next()) in order to use rs.getxxx()?
Yes, you have to move the cursor to the first row of the ResultSet before you can get information from that row.
How to get the exact row WITHOUT looping the entire database table?
I don't quite understand. You update a record with a value X, then you want to read that record back so you can get X? What for? You already have the value in memory. At any rate I don't understand what you mean by "looping the entire database table".
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yes, you always need to call rs.next to move it to the first item in the result. Otherwise you ae in the 'before first' position and there is nothing available
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Originally posted by Paul Clapham:
I don't quite understand. You update a record with a value X, then you want to read that record back so you can get X? What for? You already have the value in memory. At any rate I don't understand what you mean by "looping the entire database table".


I update the record and want to get that same record back is because the value in existing memory is not updated, I must setAttribute again to change the value in memory and somewhere else getAttribute to reflect the new value. If I don't setAttribute again, I will be getting the old value which is not correct. This is why I need to loop the db to get the updated value before doing setAttribute.

Suppose I have 1000 records in the db table. I just want to get the data from row 500 (the number 500 I don't know in advance). If I do while(rs.next()) then I must loop/check 500 times before I get the 500th record. This looping may become a performance issue. Even my query uses a where clause and limit the result to 1 eg "select * from table where key=value limit 1"
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by K. Tsang:
I update the record and want to get that same record back is because the value in existing memory is not updated, I must setAttribute again to change the value in memory and somewhere else getAttribute to reflect the new value. If I don't setAttribute again, I will be getting the old value which is not correct. This is why I need to loop the db to get the updated value before doing setAttribute.
Well, it's true that you don't save the value of req.getParameter("depositAmount") anywhere. But you could assign that to a variable. Then you wouldn't need to read it back from the database.
Suppose I have 1000 records in the db table. I just want to get the data from row 500 (the number 500 I don't know in advance). If I do while(rs.next()) then I must loop/check 500 times before I get the 500th record. This looping may become a performance issue. Even my query uses a where clause and limit the result to 1 eg "select * from table where key=value limit 1"
Yes, I thought that was kind of strange too. There might be several rows with that e-mail address, but you pick one of them at random and update it. So yeah, there isn't any way you will be able to read that same row back from the database. Not unless the e-mail address is a unique key to the table.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: session and ResultSet.getDouble
 
Similar Threads
Can't use @SqlResultSetMapping
session and ResultSet.getDouble
Visual Web JSF Navigation... Help!
InjectionException / FacesException - JNDI
JPA:- java.sql.SQLException : No database selected Error Code: 1046