[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

JDBC Result Set into a mulidimensional array

Posted on 2001-06-06
5
Medium Priority
?
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 495 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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 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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

650 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