Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with getParameterValues and converting to int[]

843835Jan 11 2003 — edited Oct 6 2005
Hello everyone,

I've created a simple JSP which inserts multiple records into a database from a standard HTML form. Initially, I only used arrays of string values (name and gender), to see if these would work using request.getParameterValues("paraname") and they did.

Now, I need to insert arrays of int values into the database but I can't use int[] age = Integer.parseInt(requestParameterValues("age")) to convert from a string to int.

I read posting http://forum.java.sun.com/thread.jsp?forum=48&thread=87069 which provided a solution and I have implemented in my JSP (see below) but I get 2 errors as below:


--------------------- Error ---------------------

Apache Tomcat/4.0.3 - HTTP Status 500 - Internal Server Error

type Exception report

message Internal Server Error

description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.


An error occurred between lines: 12 and 46 in the jsp file: /insertNewEmployee.jsp

Generated servlet error:
C:\jakarta-tomcat-4.0.3\work\localhost\test\insertNewEmployee$jsp.java:83: Incompatible type for constructor. Can't convert java.lang.String[] to java.lang.String.
integers = new Integer(age);
^


An error occurred between lines: 12 and 46 in the jsp file: /insertNewEmployee.jsp

Generated servlet error:
C:\jakarta-tomcat-4.0.3\work\localhost\test\insertNewEmployee$jsp.java:84: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
ints = Integer.parseInt(age);
^
2 errors, 1 warning


--------------------- employeeForm.html ---------------------

<html>
<body>

<form action="insertNewEmployee.jsp" method="post">
<table>
<tr>
<td>Name</td>
<td>Gender</td>
</tr>
<tr>
<td><input type="text" name="name" size="25"></td>
<td><input type="text" name="gender" size="25"></td>
<td><input type="text" name="age" size="3"></td>
</tr>
<tr>
<td><input type="text" name="name" size="25"></td>
<td><input type="text" name="gender" size="25"></td>
<td><input type="text" name="age" size="3"></td>
</tr>
<tr>
<td><input type="text" name="name" size="25"></td>
<td><input type="text" name="gender" size="25"></td>
<td><input type="text" name="age" size="3"></td>
</tr>
</table>
<input type="submit" value="Save">
<input type="reset" value="Clear">
</body>
</html>


--------------------- insertNewEmployee.jsp ---------------------

<%@ page import="java.sql.*" %>

<%!
// Declared variables with an initial value of null
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
%>

<%
try {
String[] name = request.getParameterValues("name");
String[] gender = request.getParameterValues("gender");

String[] age = request.getParameterValues("age");
Integer[] integers = new Integer[age.length];
int[] ints = new int[age.length];
for (int a=0; a<age.length; a++) {
integers = new Integer(age);
ints = Integer.parseInt(age);
}

Class.forName("org.gjt.mm.mysql.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/test?user=assad&password=pwd");

String query ="INSERT INTO employee (NAME,GENDER,AGE) VALUES (?,?,?) ";

ps = con.prepareStatement(query);

for (int i=0;i<name.length;i++) {
ps.setString(1, name);
ps.setString(2, gender[i]);
ps.setInt(3, ints[i]);
ps.addBatch();
}
int[] results = ps.executeBatch();
ps.close();
con.close();

} catch(Exception e) {
throw new Exception(e);
}
%>
</html>

Thanks for your help in advance.

Assad
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2005
Added on Jan 11 2003
4 comments
1,154 views