| | |
JSP: Problem with Integer Values, not assigning type correctly
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 36
Reputation:
Solved Threads: 0
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".
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):-
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
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)
<FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST"> Id: <INPUT TYPE=TEXT NAME=Id><BR><BR> Category: <INPUT TYPE=TEXT NAME=category><BR><BR> Question<BR><TEXTAREA NAME=question COLS=100 ROWS=2></TEXTAREA><BR><BR> Answer<BR><TEXTAREA NAME=answer COLS=100 ROWS=4></TEXTAREA><BR><BR> Sequence: <INPUT TYPE="int" NAME=sequence><BR><BR> <INPUT TYPE=Submit VALUE="Add to Database"> </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)
Enumeration parameters = request.getParameterNames(); if(parameters.hasMoreElements()) { String IdParam = request.getParameter("Id"); String categoryParam = request.getParameter("category"); String questionParam = request.getParameter("question"); String answerParam = request.getParameter("answer"); Integer sequenceParam = request.getParameter("sequence"); statement.executeUpdate("INSERT INTO FAQ (\"Id\",\"category\", \"question\", \"answer\", \"sequence\", \"UserId\", \"created\") VALUES ('"+IdParam+"','"+categoryParam+"','"+questionParam+"','"+answerParam+"', '"+sequence+"', '"+ session.getAttribute("theName")+"', '"+dateString+"') "); } ResultSet columns = statement.executeQuery("SELECT * FROM FAQ"); while(columns.next()) { String Id = columns.getString("Id"); String category = columns.getString("category"); String question = columns.getString("question"); String answer = columns.getString("answer"); String userId = columns.getString("userId"); String created = columns.getString("created"); 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
•
•
Join Date: Mar 2007
Posts: 36
Reputation:
Solved Threads: 0
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 :-
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??
JSP Syntax (Toggle Plain Text)
String sequenceParam = request.getParameter("sequence"); int sequence = Integer.parseInt(sequenceParam); statement.executeUpdate("INSERT INTO FAQ (\"category\", \"question\", \"answer\", \"sequence\", \"UserId\", \"created\") VALUES ('"+categoryParam+"','"+questionParam+"','"+answerParam+"', '"+sequence+"', '"+ session.getAttribute("theName")+"', '"+dateString+"' )"); }
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??
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.
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.
•
•
Join Date: Mar 2007
Posts: 36
Reputation:
Solved Threads: 0
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
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
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.
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.
•
•
Join Date: Mar 2007
Posts: 36
Reputation:
Solved Threads: 0
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.
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.
![]() |
Other Threads in the JSP Forum
- Previous Thread: JSTL compare string
- Next Thread: jsp query
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






