• 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

JDBC problems

 
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
}
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cant see in your code any declarations for Vectors loCVector , loMVector
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our databases forum.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
raghav sai,
Your post was moved to a new topic.
Starting a new thread for a new topic
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic