943,879 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 11905
  • JSP RSS
Mar 14th, 2008
0

JSP: Problem with Integer Values, not assigning type correctly

Expand Post »
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".

JSP Syntax (Toggle Plain Text)
  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):-


JSP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
Robtyketto is offline Offline
36 posts
since Mar 2007
Mar 14th, 2008
0

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

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 :-

JSP Syntax (Toggle Plain Text)
  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??
Reputation Points: 10
Solved Threads: 0
Light Poster
Robtyketto is offline Offline
36 posts
since Mar 2007
Mar 15th, 2008
-1

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

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 15th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Light Poster
Robtyketto is offline Offline
36 posts
since Mar 2007
Mar 15th, 2008
0

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

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 15th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Robtyketto is offline Offline
36 posts
since Mar 2007
Jul 29th, 2008
0

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

int IdParam = Integer.pasrseInt(request.getParameter("Id"));
I think,the above syntex will be solved ur problem.
Subhajit Majumder
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nitya_k83 is offline Offline
1 posts
since Jul 2008
Sep 9th, 2008
0

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

humm you sould use preparedStatement
instead of statement.executeUpdate
and to #7
your code do not check exception .....
Reputation Points: 6
Solved Threads: 1
Newbie Poster
haoyou2008 is offline Offline
7 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: JSTL compare string
Next Thread in JSP Forum Timeline: jsp query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC