Solved

JDBC Result Set into a mulidimensional array

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 195
mockito example issue 8 72
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 71
Running JavaFX on JDeveloper 12C 1 32
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

778 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