File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Returning mutple columns in a vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Returning mutple columns in a vector" Watch "Returning mutple columns in a vector" New topic
Author

Returning mutple columns in a vector

NamiraB Bilan
Greenhorn

Joined: Feb 16, 2004
Posts: 2
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
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
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

Joined: Feb 16, 2004
Posts: 2
Hi Seetesh
Thank you
I have emailed you.
Waiting urgently for your reply.
Kind Regards
N
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi Nameera,
Let me know if ur facing any problems with the code sent to ur id.
Seetesh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning mutple columns in a vector
 
Similar Threads
DATABASE OBJECT-- URGENT URGENT
OutOfMemory error
nullpointerexception
Invalid Descriptor Index
accessdata using excel