Solved

JDBC Result Set into a mulidimensional array

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

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 24

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 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

13 Experts available now in Live!

Get 1:1 Help Now