Solved

Build and load array.

Posted on 2001-06-19
13
254 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Bekkerus
Comment Utility
How do I load the array?
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
//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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Bekkerus
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now