• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

session and ResultSet.getDouble

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic