• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to put result of ResultSet in a Array

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adeel, Whats VO ? A vector ?
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shailesh, Thanks for the link.
 
Shailesh Chandra
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic