Solved

JDBC Result Set into a mulidimensional array

Posted on 2001-06-06
5
175 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 86
MySqlDump not dumping triggers 1 43
hibernate jars 4 45
How to determine if a string is a valid SHA value 7 35
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 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