Solved

JDBC Result Set into a mulidimensional array

Posted on 2001-06-06
5
167 Views
Last Modified: 2010-04-16
Given this code snippet:

ResultSet myRs = stmt.executeQuery(
  "SELECT COLA,  COLB, COLC FROM TABLE_A");

/*  Process the Result Set INTO AN ARRAY */

String[][] myTable = new String[2][3];

while (myRs.next ()) {

}

How do I process the rows in the result set myRs into the matrix myTable?

Given that I know the number of rows in the result set and the number of columns in the select list so I know the matrix will be 2 rows by 3 columns.
0
Comment
Question by:j_murabito
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 165 total points
ID: 6160248
Hi!

Check out this link
http://java.sun.com/products/jdk/1.1/docs/api/java.sql.ResultSet.html#_top_

This how you can do it
String[][] myTable = new String[2][3];
int i = 0;
int j = 0;
while(myRS.next()){
  myTable[i][0] := myRS.getString("<field name>");
  // or
  myTable[i][1] := myRS.getString(1); //column number
  myTable[i][2] := myRS.getString(2); //column number
  i++;
}

Regards,
Tomas Helgi
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6160294
ResultSet myRs = stmt.executeQuery(
 "SELECT COLA,  COLB, COLC FROM TABLE_A");

/*  Process the Result Set INTO AN ARRAY */

String[][] myTable = new String[2][3];
int i = 0;
while (myRs.next ()) {
myTable[i][0] = myRs.getString("COLA");
myTable[i][1] = myRs.getString("COLB");
myTable[i][2] = myRs.getString("COLC");
i++;
if(i>1) // for the case of more than two rows to avoid ArrayIndexOutOfBoundsException
break;
}


*****************************************
or, if you have more than 2 rows do'it in this way :
ResultSet myRs = stmt.executeQuery(
 "SELECT count(COLA) FROM TABLE_A");
int count = 0;
//retriving the max row count number
while (myRs.next ()) {
count = myRs.getInt(1);
}

/*  Process the Result Set INTO AN ARRAY */

ResultSet myRs1 = stmt.executeQuery(
 "SELECT COLA,  COLB, COLC FROM TABLE_A");

/*  Process the Result Set INTO AN ARRAY */

String[][] myTable = new String[count][3];
int i = 0;
while (myRs1.next ()) {
myTable[i][0] = myRs1.getString("COLA");
myTable[i][1] = myRs1.getString("COLB");
myTable[i][2] = myRs1.getString("COLC");
i++;
}

0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 6160334
Hi!

while (myRs.next ()) {
count = myRs.getInt(1); // (1)
}

(1) this line could produce error if the column of the table is not an integer field.

Better way is to use: count++;

Regards,
Tomas Helgi

0
 

Author Comment

by:j_murabito
ID: 6160892
Tomas:

Thanks for your help.

Joseph M
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6162499
mr. TOMASHELGI : if you take a closer look at the first sql statement you will find there something like 'select count(COLA)...' which of course is an integer column. If you don't know about the count('column') SQL function, please consult some serious documentation and so you'll be able to give corect answears and comments to the questions posted here.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 52
Unable to open debugger port in Intellij idea 6 139
Image decoding from Camera 3 72
stackato and cloud 4 64
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

932 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

12 Experts available now in Live!

Get 1:1 Help Now