It's not a secret anymore!*
The moose likes Servlets and the fly likes Servlet Displays Old DB Entries  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Displays Old DB Entries  " Watch "Servlet Displays Old DB Entries  " New topic
Author

Servlet Displays Old DB Entries

Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
hello, I made this code which simply fetches the data and each row has a Edit link Besides it...
When i click on Edit, the record appears perfectly, but when i make changes and click on save, it refreshes but shows old (unchanged) record.


Table - Qtable



code For Edit page..

Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi Gaurav,

I think the code is for the edit screen. It doesnt have any details of your save operation.

You need to have a code to save the edited record. Its only having select statement. So once you edit and save some insert should go through.


Regards,
Sudipta Kumar Laha
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68



I missed out the essential part....the code shows on error on execution, but doesnt update the record
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

What error it shows? it shows an error or an exception?


Regards, Prasad
SCJP 5 (93%)
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
The first step is to print the actual SQL statement that you're passing to the DB; then you can run that directly against the DB and see what happens.
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

SQL statement seems to be correct.
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi,

I don't think update will happen with this code. The sequence of the code is as follows:

When the screen loads:
1. A select statement is executed and the q2-q7 gets populated
2. the q2-q7 values are painted on the screen from the servlet.
3. Then an update statement executes with the same q2-q7 values(user havent modified anything in the screen yet).
4. Screen loading is complete and now the user can change the values.

User operation:
1. user changes the values and submits.

Again we are calling the same servlet:
1. A select statement is executed and the q2-q7 gets populated
2. the q2-q7 values are painted on the screen from the servlet.
3. Then an update statement executes with the same q2-q7 values(not the user modified values-- its all the original values in DB that we got from the select statement).
4. Screen loading is complete.

So data are not saved in the DB.

Solution:

1. In the starting get the values(q2-q7) from the request using getParameter().
2. If they are not null(primary key is not null or any condition which distinguish new screen or edit screen) execute update statement.
3. After that the code flow remains same(select and then HTML painting). The update statement in the below part is not required.




Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
@Prasad : i made a mistake, wanted to write *shows no error on execution.
@lester : i didnt get you.

@Sudipta : Thank you so much, your explanation made things really clear and now its working.

But just a bit of a problem now. When i click on Submit Query, it shows "null" in every text box.





Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49


I believe the update should be on the top.
Reason: Suppose you update the details and submit. The select statement goes before the update is done to the DB and it will show the old data when the screen loads.

And for the null value, check the values in the resultset by giving SOP.
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
i checked value of result set...it shows true

And cant figure out how to get Update on top, because before it fetches the data using SELECT, if it executes UPDATE then it will set all the values to "null".

Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
hi...

Its depends on you how you want to handle it.

1. You may always run a update query when the form is submited , by giving a param like mode.

2. you may run two select query one before update to check your update condition and the 2nd one to show the updated data.
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
hi...
i couldnt understand how to implement your first point..

And when i used 2nd , it didnt work.

If i use the ResultSet (Using Same Object of Statement ) statement to execute Select two times i get "java.sql.SQLException: ResultSet is closed"

And If i declare the A new Statement object for using ResultSet - i get "No data found" for which the code is

Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi,

You need to understand the sequence. The code is not having the correct flow.
Take a look at the operations you are doing. The last select is of no used as you dont use the data.

1. It should be select 1st.
2. Then there should be an update.
3. Then there should be select again to get updated data.
4. Then there should be the HTML code to display the data to UI.
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
WOW! that worked

Thank you mate

If you would notice the code, I added another condition to check if the values were null. Without that condition...if i would click on EDIT, It will fetch and update each attribute with "null". That is because
1. It gets values for a record using SELECT
2. It Checks if there is any change in it, if there is then it Causes UPDATE to run. Since the Update statement is above HTML code, Hence the parameter values initially are "null" which Satisfies the condition to run UPDATE. The Update statement would then make the values Null.
3. Another Select to get the Updated Values.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Displays Old DB Entries
 
Similar Threads
After UPDATE Statement, Null Value is fetched
Problem with JSPs in web application
user authentication and dao
Getting error "Oops! This link appears to be broken."
Hello world ejb3 problem