aspose file tools*
The moose likes JDBC and the fly likes How to put result of ResultSet in a Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to put result of ResultSet in a Array" Watch "How to put result of ResultSet in a Array" New topic
Author

How to put result of ResultSet in a Array

Sanam Kumar
Greenhorn

Joined: Jun 04, 2004
Posts: 9
Hello,

I am badly in need of help and it is very urgent.
can someone ppleaseeeeeeeeeeee help

I want to store the result of a resultset in a array,

Eg : i have table tblannotation , n have 3 records with id = 1

and i want to put the result in an array, how can i do that

i.e i want
annoTextArray[1] = "aaaa";
annoTextArray[2] = "bbb";
annoTextArray[3] = "aaaa";

how do i put result of a resultset in an array

pleaseeeeeeeeeeeeee help;

Thanks
Sanam


Class.forName("org.postgresql.Driver");
url1 = "jdbc ostgresql://192.161.0.0:3333/emo";
con1 = DriverManager.getConnection(url1,"an","jwqek");
stmt1 = con1.createStatement();

sql1 = "select * from tblannotation where id =1";

ResultSet rs1 = stmt1.executeQuery(sql1);
rs1.last();
int rowCount = rs1.getRow();
System.out.println("Row Count : " +rowCount);

while(rs1.next())
{
String[] anoTxt = rs1.getString("annotationtext");
//System.out.println(anoTxt1);
annoTextArray = anoTxt1;
}
[ March 03, 2005: Message edited by: Bear Bibeault ]
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
If i come across a situation like this my solution is.

Create a data structure ( A Normal class ) to hold the details of a single row. ( For example if it's a emp table create a emp Class )
For each row in the RecordSet Instantiate & set the value of all members in this class & add it to an array or collection.

But Is this a right way of design for this situation, any experianced people can throw some light.
[ March 02, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Srinivasa Raghavan:
If i come across a situation like this my solution is.

Create a data structure ( A Normal class ) to hold the details of a single row. ( For example if it's a emp table create a emp Class )
For each row in the RecordSet Instantiate & set the value of all members in this class & add it to an array or collection.

But Is this a right way of design for this situation, any experianced people can throw some light.


Surely a better way is to use a VO over Collection.
Thanks.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Adeel, Whats VO ? A vector ?
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Srinivasa Raghavan:
Adeel, Whats VO ? A vector ?


VO means Value Object, It is a Pattern, you can refer
here

alternatively you can refer DTO (Data Transfer Object)
[ March 03, 2005: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Shailesh, Thanks for the link.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Sanam,

here is solution for you,assuming you have query like

Select,col1,col2, col3 from tblannotation where id =1

create a VO as below




now use Same TableVO in your code



now use Same TableVO in your code

//If you want data in array then

TableVo[] anoTxt = (TableVo[])lstTableVO.toArray(new TableVo[lstTableVO.size()]);

// for fetching any data


String col1Value = ((TableVo) anoTxt[your_index]).getCol1();




hope this will solve your problem

Shailesh
[ March 03, 2005: Message edited by: Shailesh Chandra ]
Lee Barney
Ranch Hand

Joined: May 07, 2003
Posts: 37
Here is an example that returns an ArrayList of ArrayLists. Using arrays is not much different. fieldCount is retained information from the creation of queryString.


Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
The diaz project (diaz.sourceforge.net) demonstrates another approach for JDBC programming.

The main class in the project is net.sf.diaz.AbstractJdbcDao

To execute a query:

1) implement a class that extends AbstractJdbcDao
2) in the class, call executeQuery

The executeQuery method returns a List of net.sf.diaz.Row objects

Caveat: the diaz project is designed for use with J2SE 5.0
The code will not compile with earlier versions of the J2SE platform
 
Don't get me started about those stupid light bulbs.
 
subject: How to put result of ResultSet in a Array