| Author |
JDBC problems
|
Prabhudatta Choudhury
Greenhorn
Joined: Jul 03, 2009
Posts: 20
|
|
Hi,
I am facing a problem in simple JDBC. I have one class where I have declared a String variable to collect some data form DB(Oracle 9i). I want to reuse those data out side of the method but inside of the same class. But its returning null. please verify the code and let me know where I can improve.
<Code>
public class Test{
public Vector ogetData(){
try{
Connection loConnection = null;
Statement loStmt = null;
ResultSet loRS = null;
loConnection = ogetConnection(CTPUIConstants.CONNECTION_POOL_NAME); // I am collecting this connection object from a connection pool.I am damn sure those codes are find with no error and exception.
String message = "";
StringBuffer loQuery = new StringBuffer();
loQuery.append("SELECT CCY_CODE,MARKET_CODE,AUTHORIZATION_ID,INCLUDE_IN_TP,INCLUDE_IN_LP FROM TB_LP_DERIVED_CALENDAR ");
loStmt = loConnection.createStatement();
loRS = loStmt.executeQuery(loQuery.toString());
while(loRS.next())
{
Vector loCVector = loRS.getString(1);
Vector loMVector = loRS.getString(2);
boolean lbITP = loRS.getString(3);
boolean lbILP = loRS.getString(4);
message = loCVector .toString()+loMVector .toString()+lbITP .toString()+lbILP .toString();
}}
catch (Exception e){e.printStackTrace();}
finally{
lors.close();
loStmt.close();
loConnection.close();
}
return loCVector ;
return loMVector ;
}
public void printMessge(){
System.out.println("The message from DB is :" +message);
}
public inti(){
Test t1 = new Test();
t1.ogetData();
t1.printMessage();
}
<Code>
When I tried to print the value of that String "message" its getting blank. I want those values to be append in that. please suggest.
}
|
Thanks
Prabhudatta
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
The String variable message is declared as a local variable in the ogetData() method. To access the message variable outside the ogetData() method, in other methods of the class, then the String variable should be a class variable. Make sure to remove the declaration of the message local variable - since a local variable masks the class variable, the instance variable you newly declared will not be assigned a value in the below line -
See below code on further explanation for local & instance variable sample - uncomment the message variable declaration and run again.
Also please use code tags when you post the code. Its not using <code> </code> but [code] and corresponding closure... UseCodeTags
|
 |
Sherif Shehab
Ranch Hand
Joined: Mar 05, 2007
Posts: 472
|
|
|
I cant see in your code any declarations for Vectors loCVector , loMVector
|
Thanks,
Sherif
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
Moving to our databases forum.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
raghav sai, Your post was moved to a new topic. Starting a new thread for a new topic
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
 |
|
|
subject: JDBC problems
|
|
|