This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC problems

 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
}
 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant see in your code any declarations for Vectors loCVector , loMVector
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our databases forum.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav sai,
Your post was moved to a new topic.
Starting a new thread for a new topic
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic