JSP: Problem with Integer Values, not assigning type correctly

Thread Solved

Join Date: Mar 2007
Posts: 36
Reputation: Robtyketto is an unknown quantity at this point 
Solved Threads: 0
Robtyketto Robtyketto is offline Offline
Light Poster

JSP: Problem with Integer Values, not assigning type correctly

 
0
  #1
Mar 14th, 2008
Greetings,

Within my jsp I have HTML code (see below) which accepts input, one of these fields sequence unlike the others is an Integer and within the HTML FORM the INPUT TYPE is set to "int".

  1. <FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST">
  2. Id: <INPUT TYPE=TEXT NAME=Id><BR><BR>
  3. Category: <INPUT TYPE=TEXT NAME=category><BR><BR>
  4. Question<BR><TEXTAREA NAME=question COLS=100 ROWS=2></TEXTAREA><BR><BR>
  5. Answer<BR><TEXTAREA NAME=answer COLS=100 ROWS=4></TEXTAREA><BR><BR>
  6. Sequence: <INPUT TYPE="int" NAME=sequence><BR><BR>
  7. <INPUT TYPE=Submit VALUE="Add to Database">
  8. </FORM>


These values are then retrieved in the same jsp file using the code below and inserted into the table (sequence field is Number format in MS acess table):-


  1. Enumeration parameters = request.getParameterNames();
  2.  
  3. if(parameters.hasMoreElements()) {
  4. String IdParam = request.getParameter("Id");
  5. String categoryParam = request.getParameter("category");
  6. String questionParam = request.getParameter("question");
  7. String answerParam = request.getParameter("answer");
  8. Integer sequenceParam = request.getParameter("sequence");
  9. statement.executeUpdate("INSERT INTO FAQ (\"Id\",\"category\", \"question\", \"answer\", \"sequence\", \"UserId\", \"created\") VALUES ('"+IdParam+"','"+categoryParam+"','"+questionParam+"','"+answerParam+"', '"+sequence+"', '"+ session.getAttribute("theName")+"', '"+dateString+"') ");
  10. }
  11. ResultSet columns = statement.executeQuery("SELECT * FROM FAQ");
  12. while(columns.next()) {
  13. String Id = columns.getString("Id");
  14. String category = columns.getString("category");
  15. String question = columns.getString("question");
  16. String answer = columns.getString("answer");
  17. String userId = columns.getString("userId");
  18. String created = columns.getString("created");
  19. Integer sequence = columns.getInt("sequence");

The jsp falls over with error regarding sequence as It cant convert between String to Integer and Int to Integer.

I've been looking at examples, but obviously I arent declaring the sequence value as an Integer properly.

Any advice would be appreciated
Cheers Rob
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 36
Reputation: Robtyketto is an unknown quantity at this point 
Solved Threads: 0
Robtyketto Robtyketto is offline Offline
Light Poster

Re: JSP: Problem with Integer Values, not assigning type correctly

 
0
  #2
Mar 14th, 2008
I have resolved the initial problem of inserting the field sequence as an integer instead of a string, I changed it form a string to an integer using the code below :-

  1. String sequenceParam = request.getParameter("sequence");
  2. int sequence = Integer.parseInt(sequenceParam);
  3.  
  4. statement.executeUpdate("INSERT INTO FAQ (\"category\", \"question\", \"answer\", \"sequence\", \"UserId\", \"created\") VALUES ('"+categoryParam+"','"+questionParam+"','"+answerParam+"', '"+sequence+"', '"+ session.getAttribute("theName")+"', '"+dateString+"' )");
  5. }

Is there a better method to use? Suprised to see I need to use '" around the value on the INSERT statement as I thought this was only for STRINGS??
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JSP: Problem with Integer Values, not assigning type correctly

 
0
  #3
Mar 15th, 2008
yes, NEVER use Java scriptlets in JSP code.
NEVER do database operations from JSP.
ALWAYS use PreparedStatement for database operations.
ALWAYS use parameterised queries for database operations.

So basically restart from scratch.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 36
Reputation: Robtyketto is an unknown quantity at this point 
Solved Threads: 0
Robtyketto Robtyketto is offline Offline
Light Poster

Re: JSP: Problem with Integer Values, not assigning type correctly

 
0
  #4
Mar 15th, 2008
Thanks for the advice I have done the following two:-

ALWAYS use PreparedStatement for database operations.
ALWAYS use parameterised queries for database operations.


Unfortunately I wont be doing the other points
:-( , I'm a university student and have to follow the module structure and have to do the other things.

Thanks for the advice
Rob
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JSP: Problem with Integer Values, not assigning type correctly

 
0
  #5
Mar 15th, 2008
if you're really a university student you above all have to use your own judgement and think for yourself.
When presented with a bad example you should have the intelligence to detect that and not follow it blindly.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 36
Reputation: Robtyketto is an unknown quantity at this point 
Solved Threads: 0
Robtyketto Robtyketto is offline Offline
Light Poster

Re: JSP: Problem with Integer Values, not assigning type correctly

 
0
  #6
Mar 15th, 2008
I do understand what youre saying but due to the fact I'm against very tight deadlines, I need to go for the marks i.e. get the primary requirements finished, which unfortunately means I dont have the time to rewrite and re-test all of my code.

Much like many modules I've done I wish I had more time to investigate/explore more (12 wks modules).

Thanks for you input it hasnt fallen on death ears.
Last edited by Robtyketto; Mar 15th, 2008 at 4:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: nitya_k83 is an unknown quantity at this point 
Solved Threads: 0
nitya_k83 nitya_k83 is offline Offline
Newbie Poster

Re: JSP: Problem with Integer Values, not assigning type correctly

 
0
  #7
Jul 29th, 2008
int IdParam = Integer.pasrseInt(request.getParameter("Id"));
I think,the above syntex will be solved ur problem.
Subhajit Majumder
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 7
Reputation: haoyou2008 is an unknown quantity at this point 
Solved Threads: 1
haoyou2008 haoyou2008 is offline Offline
Newbie Poster

Re: JSP: Problem with Integer Values, not assigning type correctly

 
0
  #8
Sep 9th, 2008
humm you sould use preparedStatement
instead of statement.executeUpdate
and to #7
your code do not check exception .....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the JSP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC