Form and Integer field in database

S

Steven

Hi all,

I have a webform with a pulldown menu with some number in.
When this form is submitted it should put the entered data into a database.

I tried this SQL statement :
------
PreparedStatement st = conn.prepareStatement("INSERT INTO reports (rid,
problem_typeID, description"
+ "values(?,?,?)");
st.setInt(1, 1);
st.setString(2, request.getParameter("problem_typeID"));

st.setString(3, request.getParameter("description"));
------
But this doesn't work.
The field 'problem_typeID' in the database is an Integer field.





Also tried :
------
PreparedStatement st = conn.prepareStatement("INSERT INTO reports (rid,
problem_typeID, description"
+ "values(?,?,?)");
st.setInt(1, 1);
st.setInt(2, request.getParameter("problem_typeID"));

st.setString(3, request.getParameter("description"));
------
Also doesn't work.



Replacing the line :
st.setInt(2, request.getParameter("problem_typeID"));
with for example :
st.setInt(2, 12);
Does put in the value 12 into the database.

Any idea what is wrong and how to solve this?

Thanks for any help!!
 
M

Malte

Steven said:
Hi all,

I have a webform with a pulldown menu with some number in.
When this form is submitted it should put the entered data into a database.

I tried this SQL statement :
------
PreparedStatement st = conn.prepareStatement("INSERT INTO reports (rid,
problem_typeID, description"
+ "values(?,?,?)");
st.setInt(1, 1);
st.setString(2, request.getParameter("problem_typeID"));

st.setString(3, request.getParameter("description"));
------
But this doesn't work.
The field 'problem_typeID' in the database is an Integer field.





Also tried :
------
PreparedStatement st = conn.prepareStatement("INSERT INTO reports (rid,
problem_typeID, description"
+ "values(?,?,?)");
st.setInt(1, 1);
st.setInt(2, request.getParameter("problem_typeID"));

st.setString(3, request.getParameter("description"));
------
Also doesn't work.



Replacing the line :
st.setInt(2, request.getParameter("problem_typeID"));
with for example :
st.setInt(2, 12);
Does put in the value 12 into the database.

Any idea what is wrong and how to solve this?

Thanks for any help!!

I think what happens is that request.getParameter() returns a String,
not an int.

Try this:

int anInt = 0;

try {
anInt = Integer.parseInt(request.getParameter("problem_typeID"));
st.setInt(2, anInt);
} catch (NumberFormatException nfex) {
// you fill in the rest
} catch (NullPointerException nx) {
// you fill in the rest
}

Your actual code might benefit form being split up somewhat, my personal
preference is to keep database related stuff in its own class and keep
all the http stuff away from it, so I would have a method or a class to
do all thr request.getParameter() stuff and then pass the results off to
the database class. This leaves the error handling in one place.
 
L

Lee Fesperman

Steven said:
Hi all,

I have a webform with a pulldown menu with some number in.
When this form is submitted it should put the entered data into a database.

I tried this SQL statement :
------
PreparedStatement st = conn.prepareStatement("INSERT INTO reports (rid,
problem_typeID, description"
+ "values(?,?,?)");
st.setInt(1, 1);
st.setString(2, request.getParameter("problem_typeID"));

st.setString(3, request.getParameter("description"));

Sounds like your DBMS is weak in conversion capabilities in this area. You'll probably
need to do the conversion yourself ...
Also tried :
------
PreparedStatement st = conn.prepareStatement("INSERT INTO reports (rid,
problem_typeID, description"
+ "values(?,?,?)");
st.setInt(1, 1);
st.setInt(2, request.getParameter("problem_typeID"));

st.setString(3, request.getParameter("description"));

It won't even compile.
Replacing the line :
st.setInt(2, request.getParameter("problem_typeID"));
with for example :
st.setInt(2, 12);
Does put in the value 12 into the database.

Any idea what is wrong and how to solve this?

Yep, I think I covered what was wrong. To solve it, convert the parameter to integer.
For instance, you can use Integer.parseInt().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top