0

Hi All,

How to store the resultset value in a array? I have a field "prodvalue" in DB Table, now i want to store this "prodvalue" in array. Am able to print the values of "prodvale" in jsp, but not able to store in array.

Pl. find below the code, so that you can understand more easily.

rs is the resultset
while(rs.next())
{
String strprodvalue=rs.getString("prodvalue");
out.println(strprodvalue);
}

The above code displays all the prodvalues, but in want to store strprodvalue in array. How to do this??

Thanks,

5
Contributors
17
Replies
30
Views
7 Years
Discussion Span
Last Post by pradeepktg
0

Use ArrayList (or any other Collection type you prefer) instead of Array. For array you would have to run extra query determinate number of elements that will come out to give you array size. That you would have to initialize array with that size and only after that you could execute query to retrieve data.

-1

Try this:

//rs is the resultset

ArrayList ar=new ArrayList();

while(rs.next())
{
    String strprodvalue = rs.getString("prodvalue");
    out.println(strprodvalue);
    ar.add(strprodvalue);
}

ar is an ArrayList that has all of the values. Note that you don't need to know how many values there are ahead of time.

Votes + Comments
Next time read previous replies and do not give out answer so easily. People need to learn
0

Do NOT use scriptlets in JSP. Use a servlet instead.
And NEVER (I assume you want to, it's almost always on beginners' minds....) retain life database objects between requests.

0

Try this:

//rs is the resultset

ArrayList ar=new ArrayList();

while(rs.next())
{
    String strprodvalue = rs.getString("prodvalue");
    out.println(strprodvalue);
    ar.add(strprodvalue);
}

ar is an ArrayList that has all of the values. Note that you don't need to know how many values there are ahead of time.

Hey genevish,

Thanks! It works fine. but i want to know the length of the array and i need to compare the values in the array. How to achieve this?

0

Hi All,

Thanks for ur input! Thanks to peter_budo for providing me solution and thanks to genevish for solution n code. But guys, i need to store "strprodvalue" values in array and i need to find the lenght of that array and i need to compare the values.

Edited by pradeepktg: n/a

0

You can do all above with ArrayList. However if you insist no conversion and working with array then, ArrayList class documentation will tell you that you are provided with method size(), toArray() or with toArray(T[] a)

Hey peter_budo,

Can u give me sample code for the above case? It would be great help.

0
//rs is the resultset

ArrayList ar=new ArrayList();

while(rs.next())
{
    String strprodvalue = rs.getString("prodvalue");
    out.println(strprodvalue);
    ar.add(strprodvalue);
}

int size = ar.size();
String[] prodValues = ar.toArray();
0
//rs is the resultset

ArrayList ar=new ArrayList();

while(rs.next())
{
    String strprodvalue = rs.getString("prodvalue");
    out.println(strprodvalue);
    ar.add(strprodvalue);
}

int size = ar.size();
String[] prodValues = ar.toArray();

Thanks! But am getting this error "Type mismatch: cannot convert from Object[] to String[]"

0

Thanks! But am getting this error "Type mismatch: cannot convert from Object[] to String[]"

I always forget the casting in Java. Line three should be:

ArrayList<String> ar=new ArrayList<String>();

0

Hey guys,

Special thanks to peter_budo and genevish. After several fights, it wroks fine. Again am facing problem in comparing values between two arraylist. I have successfully converted array list into array. If i use for loop, 1st array values are prints properly, in 2nd array only the 1st element is printed. Just heck the below link.

prodValues = (String[])arr.toArray(new String[arr.size()] );
prodValues1 = (String[])ar.toArray(new String[ar.size()] );

 for(int j=0;j<=prodValues.length;j++)
	  {
                        strabc=prodValues[j];
			 prodValuesint=Float.parseFloat(strabc);
		 for(int i=0;i<=prodValues1.length;i++)
			  {
                     	  strabc1=prodValues1[i];
                         prodValuesint1=Float.parseFloat(strabc1);
if(prodValuesint1==prodValuesint)
{
out.println("Unique");
}
else if(prodValuesint1!=prodValuesint)
{
out.println("Not Unique");
}
			 }
					
	  }

My case is, i have stored unique values in "prodValues" and converting that as array and am converting each array element into float, above procedure is done for prodValues1 also. prodValues1 is the particular user values.

How to compare this?

0

What are you trying to do? See which values are repeated in the arrays?

I am just trying to compare the values between prodValues and prodValues1. Eg, if 0.1 is in prodValues and if same value also in prodValues1, i'll print that as "Unique" else "not unique".

0

im assuming your query selects all prodvalues given certain parameters in the select statement as opposed to just a single value.

in order to store the values into an array simply using the following:

// Create your ResultSet
ResultSet rs= statement.executeQuery(query);

// Declare counter variables
int c = 0;
int x = 0;

// Count number of results retrieved
while(rs.next()){
    c ++;
}

// Create a new string array with given size 'c'
String[] strprodvalue= new String[c];

// Reset your result set to the beginning
rs.beforeFirst();

// iterate through your ResultSet, adding values to the array
while(rs.next()){
    strprodvalue[x] = rs.getString("prodvalue");
    x++;
}

If you wish to then get the size of the array, you may use strprodvalue.length;

Hope this was what you were looking for...


----- sorry, i didnt see the end of the post where you wished to compare the two values ------
the following should work, please let me know if it does not

// Iterate through array PRODVALUES1
for( int x = 0; x < prodValues1.length; x++){
    
    // Iterate through array PRODVALUES2
    for( int z = 0; z < prodValues2.length; z++){
        
        // Retrieve values from arrays
        strTemp1 = prodValues1[x];
        strTemp2 = prodValues2[z];
        
        // Caste values as Float
        prodValuesInt1 = Float.parseFloat(strTemp1);
        prodValuesInt2 = Float.parseFloat(strTemp2);
        
        // Compare values
        if(prodValuesInt1 == prodValuesInt2){
            out.println("Not Unique");
        }else{
            out.println("Unique")
        } // END COMPARISON
        
    } // END PRODVALUES2 ITERATION
    
} // END PRODVALUES1 ITERATION

Edited by tyson.crouch: n/a

0

just a side note: rather than typecasting from string to float, why not simply store the values in the DB as floats and create a float array ?

just food for thought

0

Thanks Guys ! It works perfectly !!! I really appreciate the solution/idea given by you people!!!

Edited by pradeepktg: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.