Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Displays Old DB Entries

 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I missed out the essential part....the code shows on error on execution, but doesnt update the record
 
Prasad Krishnegowda
Ranch Hand
Posts: 665
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error it shows? it shows an error or an exception?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SQL statement seems to be correct.
 
Sudipta Laha
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic