Solved

Build and load array.

Posted on 2001-06-19
13
257 Views
Last Modified: 2010-04-16
Trying to make a simple 2 dimension array and load it from a resultSet. This array will be build differently depending upon how many rows are returned. I have no clue how to do this.

rs3 = stmt3.executeQuery("Select exp_line_item_id, erli_native_amt,  "
+ "erli_amt_paid, parent_id from d_bekker.EXT_aler_expense_line_item "
+ "where parent_id = (select business_object_id from d_bekker.EXT_alwf_work_item where document_id = '0510572413') "
+ "or parent_id in (select exp_line_item_id from d_bekker.EXT_aler_expense_line_item where parent_id = "
+ "(select business_object_id from d_bekker.EXT_alwf_work_item where document_id =" + CN + ")) " 
+ "order by exp_line_item_id" );
                   
int Cno = 0;
while(rs3.next())
{
     Cno = Cno + 1;
}

int x = 0;
int y = 0;
//the array is always be 3 wide but the length will vary.
float[][] FArry = new float[3][Cno];

for (int i = 0; i < Cno; i++)  
{
     // try and load this 3,4 array. 3 cols, 4 rows.
     FArry[x][y] = { rs3.getInt(1), rs3.getInt(2), rs3.getInt(3) }, {y};
     x = x + 1;
     y = y + 1;
}


Here is my test Data and how the array should be built this time:

exp_line_item_id | erli_native_amt | erli_amt_paid
------------------------
1613233004, 11.12,  11.12
1613233006, 20.65,  20.65
1613233008, 20.65,  20.56
1613233010, 396.19, 396.19
0
Comment
Question by:Bekkerus
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 4

Expert Comment

by:jerch
ID: 6207728
You know... this would not work if you have columns of different data types since array only supports single data type.

my 2 cents...
Jerson
0
 

Author Comment

by:Bekkerus
ID: 6207783
That is good. The data is numeric float can handle numbers right? Any idea on how to make it work?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6207941
1. Do a count query first to find out how many rows the resultset will contain

or

2. use getArray() to get one of the columns and then get the length of that array to determine the size of you multi-dimensional array
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Bekkerus
ID: 6207970
How do I load the array?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6208025
//get the number of rows somehow e.g :

Array a = rs3.getArray(1);
int number_of_rows = Array.getLength(a);

float[][] fArray = new float[number_of_rows][3]
int i = 0;

while (rs3.next()){
  for (int j = 1; j < 4; j++){
    fArray[i][j] = rs3.getInt(j);
  }
  i++
}
0
 

Author Comment

by:Bekkerus
ID: 6208102
I import:
import java.io.*;
import java.util.*;
import java.sql.*;
import java.sql.Array;

Array a = rs3.getArray(1);
int number_of_rows = Array.getLength(a);

float[][] fArray = new float[number_of_rows][3];
int i = 0;

while (rs3.next())
{
     for (int j = 1; j < 4; j++)
     {
          fArray[i][j] = rs3.getInt(j);
     }
     i++;
}

Error:

D:\Netscape\Server4\java>javac FixOob.java
FixOob.java:89: cannot resolve symbol
symbol  : method getLength  (java.sql.Array)
location: interface java.sql.Array
                                int number_of_rows = Array.getLength(a);
                                                          ^
1 error
0
 

Author Comment

by:Bekkerus
ID: 6208322
Tried another way to get the row count. Compiles but runtime error. How can I fix this and get it to print the results from the array?

int Cno = 0;
while(rs3.next())
{
     Cno = Cno + 1;
}

float[][] fArray = new float[Cno][3];
int i = 0;
rs3.first(); <---- error here.

while (rs3.next())
{
     for (int j = 1; j < Cno; j++)
     {
          fArray[i][j] = rs3.getInt(j);
     }
     i++;
//---> how to see results System.out.println( fArray[0][0] );
}

error message:

java.lang.ArrayIndexOutOfBoundsException
        at FixOob.TestOOB(FixOob.java:93)
        at FixOob.main(FixOob.java:13)
0
 
LVL 3

Expert Comment

by:superschlonz
ID: 6208393
Is it possible to exchange these two indexes ?

If yes you could do it like this:

Vector v = new Vector();
while( rs.next() )
{
  v.addElement( new float[]{ rs.getInt(1), rs.getInt(2), rs.getInt(3) } );
}
float[][] fArray = (float[][])v.toArray( new float[v.size()][] );

But now if you got one row you access the three values as follows:

float value1 = fArray[0][0];
float value2 = fArray[0][1];
float value3 = fArray[0][2];
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6208536
Sorry, the Array object with the static method getLength() is a java.lang.reflect.Array

Try it as :

  int number_of_rows = java.lang.reflect.Array.getLength(a);



0
 

Author Comment

by:Bekkerus
ID: 6208723
int CFN = fArray.getNumber(0,0); <--- does not work?
can I cast the value from the array into a int value?

And when I display the array to the command prompt it cuts off the values to 11.0?  they should show 2 decimal values like 1613233004, 11.12, 11.12 and 1613233006, 20.65, 20.65

Using double and float for these should have solved that right but it does not.

and this give this:
Array a = rs3.getArray(1);
int number_of_rows = java.lang.reflect.Array.getLength(a);

java.sql.SQLException: Exhausted Resultset
        at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:114)
        at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:156)
        at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:219)
        at oracle.jdbc.driver.ScrollableResultSet.getOracleObject(ScrollableResultSet.java:
        at oracle.jdbc.driver.ScrollableResultSet.getARRAY(ScrollableResultSet.java:748)
        at oracle.jdbc.driver.ScrollableResultSet.getArray(ScrollableResultSet.java:664)
        at FixOob.TestOOB(FixOob.java:140)
        at FixOob.main(FixOob.java:13)
0
 

Author Comment

by:Bekkerus
ID: 6212529
OK here is what I have found... I have something working but man is it ugly.

rs3 = stmt3.executeQuery("Select exp_line_item_id, erli_native_amt,  "
+ "erli_amt_paid, parent_id from d_bekker.EXT_aler_expense_line_item "
+ "where parent_id = (select business_object_id from d_bekker.EXT_alwf_work_item where document_id = '0510572413') "
+ "or parent_id in (select exp_line_item_id from d_bekker.EXT_aler_expense_line_item where parent_id = "
+ "(select business_object_id from d_bekker.EXT_alwf_work_item where document_id =" + CN + ")) " 
+ "order by exp_line_item_id" );
                   
int Cno = 0; //number of rows
while(rs3.next())
{
     Cno = Cno + 1; //find out how many rows there are.
}

// create array.
double[][] fArray = new double[Cno][3];

// set variables for loading array.
int k = 0; //used for column count.
int i = 0; //used for row count.

// reset rs to 0.
rs3.beforeFirst();

while (rs3.next())
{
     for (int j = 1; j < Cno; j++)
     {
          k = j - 1; //array is zero based.
          fArray[i][k] = rs3.getDouble(j);
     }
     i++;
}
                   
// Get the CNFR numbers
int x = 0;
String ConfNums;
ConfNums = "";

for (int j = 0; j < Cno; j++)
{
     x = (int)fArray[j][0];
     if (j < Cno -1 )
     {
          ConfNums = ConfNums +x+",";
     }
     else{
          ConfNums = ConfNums +x;
     }          
}

rs4 = stmt4.executeQuery("select exp_line_item_id, base_amount, native_amount "
+ "from EXT_aler_line_allocation "
+ "where exp_line_item_id in (" + ConfNums + " ) "
+ "order by exp_line_item_id" );
                   
Vector v = new Vector();
while( rs4.next() )
{
     v.addElement( new double[]{ rs4.getInt(1), rs4.getInt(2), rs4.getInt(3), rs4.getInt(4) } );
}

How do I make this better? How do I display the data in the vector... like place them into variables and print them to System.out.println?
0
 
LVL 3

Accepted Solution

by:
superschlonz earned 100 total points
ID: 6212598
OK, I make another try, let's look if you like this one:

rs3 = stmt3.executeQuery("Select exp_line_item_id, erli_native_amt,  "
+ "erli_amt_paid, parent_id from d_bekker.EXT_aler_expense_line_item "
+ "where parent_id = (select business_object_id from d_bekker.EXT_alwf_work_item where document_id= '0510572413') "
+ "or parent_id in (select exp_line_item_id from d_bekker.EXT_aler_expense_line_item where parent_id = "
+ "(select business_object_id from d_bekker.EXT_alwf_work_item where document_id =" + CN + ")) " 
+ "order by exp_line_item_id" );

// read it with row/column index swapped
Vector v = new Vector();
while( rs.next() )
{
  v.addElement( new float[]{ rs.getInt(1), rs.getInt(2), rs.getInt(3) } );
}

// swap row/column indexes
float[][] fArray = new float[3][];
fArray[0] = new float[v.size()];
fArray[1] = new float[v.size()];
fArray[2] = new float[v.size()];
for( inti=0; i<v.size(); ++i )
{
  float[] temp = (float[])v.elementAt(i);
  fArray[0][i] = temp[0];
  fArray[1][i] = temp[1];
  fArray[2][i] = temp[2];
}

// print the array
for( int y=0; y<fArray[0].length; ++y )
{
  for( x=0; x<3; ++x )
  {
    System.out.print( "  " + fArray[x][y] );
  }
  System.out.println( "" );
}
0
 

Author Comment

by:Bekkerus
ID: 6231484
This compiles and makes sence with the fewest number of lines. It was close enough to solving the problem. Had a problem building a string of column 1,2,3... row y from the fArray. But I dont want to keep pushing this question too long.

Thanks for the ideas everyone very helpful.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question