File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes JDBC problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "JDBC problems" Watch "JDBC problems" New topic

JDBC problems

Prabhudatta Choudhury

Joined: Jul 03, 2009
Posts: 20


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.


public class Test{

public Vector ogetData(){

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();

loStmt = loConnection.createStatement();
loRS = loStmt.executeQuery(loQuery.toString());

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();}


return loCVector ;
return loMVector ;

public void printMessge(){

System.out.println("The message from DB is :" +message);


public inti(){

Test t1 = new Test();



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

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: 484

I cant see in your code any declarations for Vectors loCVector , loMVector

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45277
Moving to our databases forum.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32815

raghav sai,
Your post was moved to a new topic.
Starting a new thread for a new topic

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
subject: JDBC problems
It's not a secret anymore!