Results 1 to 16 of 16
Thread: Problems while doing update
- 07-21-2010, 09:23 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
Problems while doing update
Hello All,
I am having difficulties while doing the update:
The first step is that I retrieved the data:
Then I assigned the value:XML Code:String query ="select n.nationality_name from NATIONALITY_LKUP n, cir c where n.country_code=c.nationality
The drop down list items:XML Code:Nationality= rs2.getString("nationality_name"); if (Nationality== null || Nationality.equals("")) { Nationality="";}
In the update statement:XML Code:<select size="1" name="Nationality" class="content"> <option selected value="<%=Nationality%>"><%=Nationality%></option> <% try { ResultSet rs=null; Statement st1=null; String query = "select country_code, nationality_name from nationality_lkup "; st1 = conn1.createStatement(); rs = st1.executeQuery(query); while(rs.next()) { %> <option value="<%=rs.getString("country_code")%>" > <%=rs.getString("nationality_name")%></option> <% } } catch (Exception e) { e.printStackTrace(); } %> </select>
Now while updating, instead of putting the code of the country, it is storing the whole country nameXML Code:String sz_SQLUpdate = "UPDATE cir SET"; z_SQLUpdate = sz_SQLUpdate + " , nationality ='"+Nationality+"'";
e.x, it was UK, Now United Kingdom.
Can you please help
- 07-21-2010, 04:57 PM #2
Member
- Join Date
- Jun 2010
- Location
- USA
- Posts
- 19
- Rep Power
- 0
Then you're not putting in the correct value in the update SQL statement. What is the "Nationality" variable even for? It looks like all you're doing is setting it to the value of the first result of the select statement.
Also, you should use the PreparedStatement class for when a SQL query has parameters.
Java Code:PreparedStatement ps = connection.prepareStatement("UPDATE cir SET nationality = ?"); ps.setString(1, Nationality);
- 07-21-2010, 05:18 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
How does the first bit of your post (with the query and the rs2) relate to the main chunk of JSP that you have given us?
I take it that supplies a single Nationality (variables start with a lower case by the way, class names start with an upper case) which seems to be used to give the first option for the drop down list. This doesn't make much sense to me.
What are you trying to achieve?
- 07-22-2010, 12:11 PM #4
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
you are not getting the value of the field properly in the business logic layer.
Please do check ..
And by the help of code you have provided its not possible to debug..Arun K R,Bangalore,India
:)
- 07-22-2010, 12:39 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
I am trying to update the drop down list values in the database.
Is there is an example on how to update.
Now the form has retrieved the values and place them in the drop down list.
So, now how can I update the DB.
- 07-22-2010, 12:50 PM #6
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
request.getParameter("Nationality") will give you the value of the select box.
Use prepared statement :
Java Code:PreparedStatement ps = connection.prepareStatement("update table set columnName = ?"); ps.setString(1,request.getParameter("Nationality") ); ps.executeUpdate();Arun K R,Bangalore,India
:)
- 07-22-2010, 05:17 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
Hello Arun,
This result gave me the name, So in case I want the code what shall I do?
XML Code:select size="1" name="Nationality" class="content"> <option selected value="<%=Nationality%>"><%=Nationality%></option> <% try { ResultSet rs=null; Statement st1=null; String query = "select country_code, nationality_name from nationality_lkup "; st1 = conn1.createStatement(); rs = st1.executeQuery(query); while(rs.next()) { %> <option value="<%=rs.getString("country_code")%>" > <%=rs.getString("nationality_name")%></option> <% } } catch (Exception e) { e.printStackTrace(); } %> </select>
- 07-22-2010, 05:51 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
This is what's confusing me here.
You have a <select> being built, but the options are:
The first uses Nationality for its value and display, whereas all the rest have a value based on country_code. Surely that's not correct?Java Code:<option selected value="<%=Nationality%>"><%=Nationality%></option> <option value="<%=rs.getString("country_code")%>" > <%=rs.getString("nationality_name")%></option>
- 07-22-2010, 07:33 PM #9
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
Instead of your query use the below query
Use the above code..Java Code:String query ="select n.country_code,n.nationality_name from NATIONALITY_LKUP n, cir c where n.country_code=c.nationality"; String NationalityCode = rs2.getString("n.country_code"); <select size="1" name="Nationality" class="content"> <% try { ResultSet rs=null; Statement st1=null; String query = "select country_code, nationality_name from nationality_lkup "; st1 = conn1.createStatement(); rs = st1.executeQuery(query); while(rs.next()) { %> <option value="<%=rs.getString("country_code")%>" <% if(rs.getString("country_code").equals(NationalityCode) ) out.print("selected");%> <%=rs.getString("nationality_name")%></option> <% } } catch (Exception e) { e.printStackTrace(); } %> </select>
It wil solve your problemArun K R,Bangalore,India
:)
- 07-22-2010, 10:39 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
Thank you arun
I will try this and modify the whole code since there are other 5 drop down lists in my jsp.
I hope that the value will be displayed to the user as United Kingdom and the stored will be UK
- 07-23-2010, 08:44 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
And since I've managed to bite my tongue so far, a lot of this should really be done in a servlet, or at least via tags. Large amounts of <% ... %> makes what should be a display only thing very difficult to read, and harder to maintain. Of course if you're learning Java at the moment and haven't got to tags then that's understandable.
Oh, and that code given above doesn't close its resources...another reason for not doing this in a JSP directly. It's easy to miss the flow.
- 07-23-2010, 10:00 AM #12
Hi,
One more suggestion you can't directly use ur results sets in condition checking or other things.
<option value="<%=rs.getString("country_code")%>" <% if(rs.getString("country_code").equals(Nationality Code) ) out.print("selected");%>
<%=rs.getString("nationality_name")%></option>
In the above code you will get problem,for each and every rs.getString(),record pointer will move to next record.U will not get the same one for all the above gets.
So,store it in some variable and use it.Ramya:cool:
- 07-23-2010, 10:46 AM #13
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
No RamyaSivakanth.. rs.getString() will not point to next record...
rs.next() will point to the next record..
I believe you got confused..Arun K R,Bangalore,India
:)
- 07-23-2010, 12:56 PM #14
oh sorry....really I was thinking about rs.next I hve written :-
Ramya:cool:
- 07-25-2010, 12:33 PM #15
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
Hello arun,
I have tried it in another way and success.
Thanks, but I also need some help.
In case if the user changed all the fields it will accept the change and will be updated. But, if he only changed one field, a blank "" will be shown. So, how can I deal with this?
- 08-02-2010, 06:21 AM #16
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
update problem
By PhQ in forum JDBCReplies: 6Last Post: 01-25-2011, 07:00 PM -
JRE 1.5 update 16 MSI installer
By yogi1410 in forum Java SoftwareReplies: 0Last Post: 01-22-2010, 09:49 AM -
JPanel won't update
By ibanez270dx in forum New To JavaReplies: 3Last Post: 01-06-2009, 08:59 PM -
How to update my jdk???
By low224 in forum New To JavaReplies: 4Last Post: 01-04-2009, 04:51 PM -
Using sql:update tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 11:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks