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.
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
posted
0
@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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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.