// JP opened flex table

Click to See Complete Forum and Search --> : Passing an unbounded string array from jsp to javabean


Raymond823
February 3rd, 2002, 10:24 PM
Hi!

I wanted to make a javabean which will allow the user to specify the fields of a table and use this field as a parameter in my prepared statements, for example in the jsp page:

String fieldval[];

fieldval = new String[5];
fieldval[0] = "My Name";
fieldval[1] = "23";
:
:

myBean.InsertToTable(fieldval)

my java bean code should be something like this:

public void InsertToTable(value[])
{
PreparedStatement insertValues;

String stmt = "insert into myTable values("
for (i=0; i< size of array-1; i++)
stmt = stmt + "'" + value[i] + "'" + ",";
stmt = stmt + "'" + value[size of array] + "')";
insertValues = myConn.prepareStatement(stmt);
}

my problem here is how to get the "size of array" since it will only be determined by the one writing the jsp page. I hope that I was able to explain this clearly. Thanks in advance!

Raymond823
February 4th, 2002, 12:55 AM
Here is a followup question, someone suggested using arraylist in the jsp page, however i dont know if there is a way of determining the data type of each element of the arraylist... the get method would return a type of object but I still won't be able to determine the data type to be used.

dlorde
February 4th, 2002, 04:46 AM
In Java, arrays can be queried for their length:String[] array = new String[20];
int length = array.length; // length is set to 20.

Dave

Raymond823
February 4th, 2002, 04:54 AM
Im sorry, what I mean is the number of elements in the array, not the size of the array. Thanks!

dlorde
February 4th, 2002, 05:04 AM
You can determine the type of an Object by reflection, or by using the instanceof keyword:String foo = "bar";
Object obj = foo;
if (obj instanceof String) {
String foo2 = (String)obj;
...
}

Usually, there is some convention in the coding that tells the recipient what type to expect. It is unusual to have an arrayList containing a number of different (unrelated) types.

Dave

dlorde
February 4th, 2002, 05:06 AM
Why would the size and the number of elements be different? If the array is allocated according to the number of elements required, there's no problem.

Otherwise, just ignore the null array elements...

Dave

//JP added flex table