• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning mutple columns in a vector

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I seem to have a problem with reurning rows of data, with more than 1 columns.
I am writing a generic method that returns data i get from database. The parameters I take is a query string, and connection details.
my problem is that when i select more than one column from a table.. instead of returning something like this:
1,Coal
2,Companies
3,Energy
it will return this:
1,Coal,2,Companies,3,Energy
1,Coal,2,Companies,3,Energy
1,Coal,2,Companies,3,Energy
it will display 3 times since there is only 3 dummy entries in database table for test puposes.
How can i get this right?
Any help is appreciated
Thanx
The method :
public Vector getSQLQueryData(String queryString,String url, String User, String Password){
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
Vector v = new Vector();
Vector v2 = new Vector();

try{
//getConnection()
conn = getConnection(url,User,Password);
stmt = conn.createStatement();
rs= stmt.executeQuery(queryString);

while ((rs != null) && (rs.next())) {
for(int i = 1;i <= rs.getMetaData().getColumnCount() ;i++) {
v.addElement(rs.getString(i));
}
v2.add(v);
}

} catch (SQLException sqle) {
System.out.println("SQLException :"+ sqle.getMessage());
}finally{
//close rs
try{
if(rs != null){
rs.close();
}
}
catch(Exception e){
System.out.println("Exception: RS cannot close :"+ e.getMessage());
}

//close stmt
try{
if(stmt != null){
stmt.close();
}
}
catch(Exception e){
System.out.println("Exception: stmt cannot close :"+ e.getMessage());
}
//close conn
try{
if(conn != null){
conn.close();
}
}
catch(Exception e){
System.out.println("Exception: conn cannot close :"+ e.getMessage());
}
}//end finally

return v2;
}//end getSQLQueryData(String,String,String,String)
When I call this method ...the code in main():
public static void main(String[] args){
FUtilsRS futils = new FUtilsRS();
String queryString = "select distinct id, folder from tbl_foldername order by folder";
String url = "URL";
String User = "USER";
String Password = "PASSWORD";
Vector v = new Vector();
ResultSet rs = null;
v = futils.getSQLQueryData(queryString,url,User, Password);

for(int i=0; i<v.size();i++){
System.out.println("" + i + v.get(i));
}
}//end main
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The problem lies with ur Vector defination. Ur getting a set of records for one column in vector1 and then assigning the same vector values to vectorb.
U have to define the 2nd Vector as a Vector array.
To have a code for fetching records into a Vector, send a mail to seetesh.hindlekar@sisl.co.in
Seetesh
 
NamiraB Bilan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seetesh
Thank you
I have emailed you.
Waiting urgently for your reply.
Kind Regards
N
 
Seetesh Hindlekar
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nameera,
Let me know if ur facing any problems with the code sent to ur id.
Seetesh
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seetesh,
JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.
I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.
thanks,
bear
Forum Bartender
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic