aspose file tools*
The moose likes JDBC and the fly likes getting data using column name 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 "getting data using column name" Watch "getting data using column name" New topic
Author

getting data using column name

kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
hi,
my table structure like this;

GDATE SALES1 SALES2 SALES3
12-AUG-2003 10 20 30
13-AUG-2003 20 30 40
14-AUG-2003 30 20 60
15-AUG-2003 90 100 50

COLUMN NAME SALES1 DATA SHOULD STORE LIKE THIS IN an ARRAYLIST
list.add("12-aug-2003","SALES1",10);
list.add("13-aug-2003","SALES1",20);
list.add("14-aug-2003","SALES1",30);
list.add("15-aug-2003","SALES1",90);
COLUMN NAME SALES2 DATA SHOULD STORE LIKE THIS IN an ARRAYLIST
list.add("12-aug-2003","SALES2",20);
list.add("13-aug-2003","SALES2",30);
list.add("14-aug-2003","SALES2",20);
list.add("15-aug-2003","SALES2",100);
COLUMN NAME SALES3 DATA SHOULD STORE LIKE THIS IN an ARRAYLIST
list.add("12-aug-2003","SALES3",30);
list.add("13-aug-2003","SALES3",40);
list.add("14-aug-2003","SALES3",60);
list.add("15-aug-2003","SALES3",50);
HOW WILL U ACHIEVE IN JDBC?

BYE
CHAITANYA
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Please try to explain what do you want to achieve? If you want to fetch the data from database and arrange it in the Arraylists are you shown then you can get the Column Name from the Database using the ResultSetMetaDate Class.
ResultSetMetaData mt = rs.getMetaData();
String colName = mt.getColumnName(); //this will give you the column name of Database i.e. SALES1 or SALES2 etc
If this is not your problem then explain.
kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
Originally posted by Ali Gohar:
Please try to explain what do you want to achieve? If you want to fetch the data from database and arrange it in the Arraylists are you shown then you can get the Column Name from the Database using the ResultSetMetaDate Class.
ResultSetMetaData mt = rs.getMetaData();
String colName = mt.getColumnName(); //this will give you the column name of Database i.e. SALES1 or SALES2 etc
If this is not your problem then explain.

i know that i will get the column name using ResultSetMetaData
i wrote code like this ;
while(rs.next){
if(colName.equals("SALES1"))
{
list.add(rs.getDate(1),"SALES1",rs.getInt(2));
}
else if(colName.equals("SALES2"))
{
list.add(rs.getDate(1),"SALES2",rs.getInt(3));
}
else if(colName.equals("SALES3"))
{
list.add(rs.getDate(1),"SALES3",rs.getInt(4));
}

}

but the while loop is not coming to the next else if condition;its only checking in if condition.why its not coming;other than this how can i achieve ?
bye
chaitanya
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
First of all use "()" with rs.next in your while loop. Try to implement the following code and let me know if there is any problem.
ResultSetMetaData mt = rs.getMetaData();
while(rs.next()){
int colCount = mt.getColumnCount();
for(int i=1;i<=colCount ; i++){
String colName = mt.getColumnName(i);
if(colName.equals("SALES1"))
{
list.add(rs.getDate(1),"SALES1",rs.getInt(2));
}
else if(colName.equals("SALES2"))
{
list.add(rs.getDate(1),"SALES2",rs.getInt(3));
}
else if(colName.equals("SALES3"))
{
list.add(rs.getDate(1),"SALES3",rs.getInt(4));
}
}
}
sandesh bangera
Greenhorn

Joined: Nov 13, 2002
Posts: 12
hi,
the reason its not entering the elseif is because, when you say result returns a row, that row continas all three columns in it.
therefore, every row returned by the result set has SALES1, SALES2, SALES3 in it, thereby always entering the IF condition.
replace the elseif with just if.
kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
Originally posted by sandesh bangera:
hi,
the reason its not entering the elseif is because, when you say result returns a row, that row continas all three columns in it.
therefore, every row returned by the result set has SALES1, SALES2, SALES3 in it, thereby always entering the IF condition.
replace the elseif with just if.


Thank u sandesh & Gohar for ur replies;u solved my problem;
bye
chaitanya
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: getting data using column name