wood burning stoves 2.0*
The moose likes JDBC and the fly likes JDBC Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC Connection" Watch "JDBC Connection" New topic
Author

JDBC Connection

Roger Gazdzicki
Ranch Hand

Joined: Oct 04, 2002
Posts: 47
Hello all.
I am trying to get an instance of my OracleConnection from the class OracleConnection.
The compiled class is in the same folder as the Emp class that I am trying to compile. The Emp.java class cannot recognize the OracleConnection class. I have tried to add it to my classpath with no sucess, I have tried to import the class and I have also tried to create the package with both files in the package. I figure I am doing something really stupid.
OracleConnection class
import java.sql.*;
import java.util.*;
public class OracleConnection{
private String dbURL = "";
private String dbUsername = "";
private String dbPassword = "";
private PropertyResourceBundle properties;

private final static String KEY_CONNPOOL_PROPERTIES = "connection.properties";
private final static String ANUM_DATASOURCE_URL = "myMainDatabase.datasource.url";
private final static String ANUM_DATASOURCE_USERNAME = "myMainDatabase.datasource.username";
private final static String ANUM_DATASOURCE_PASSWORD = "myMainDatabase.datasource.password";

public static OracleConnection theInstance = null;

public OracleConnection(){
}
public OracleConnection getInstance(){
if(theInstance == null)
initialize();
return theInstance;
}
/**
* intialize the instance variables.
*/
private void initialize(){
theInstance = new OracleConnection();
properties = (PropertyResourceBundle)ResourceBundle.getBundle(KEY_CONNPOOL_PROPERTIES);
dbURL = properties.getString(ANUM_DATASOURCE_URL);
dbUsername = properties.getString(ANUM_DATASOURCE_USERNAME);
dbPassword = properties.getString(ANUM_DATASOURCE_PASSWORD);
}
/**
* This method is used to get the connection to the
* ACode database.
*
* @return Connection, the connection to the ACode database.
* @exception SQLException
* exception is throw when unable to get the connection
* to the database
*/
protected synchronized Connection getConnection()throws SQLException{
return DriverManager.getConnection(dbURL, dbUsername, dbPassword);
}
protected void releaseConnection(Connection connection) throws SQLException{
connection.close();
}
}

Emp class as it stands now.
import java.sql.*;
import java.util.*;
public connectionManager = OracleConnection.getInstance;
public class Emp{
private String empNo = "";
private String eName = "";

public Emp(OracleConnection connectionManager, String empNo){
this.connectionManager = connectionManager;
this.empNo = empNo;
setEmployeeName();
}
/**
* WRITE THE ACCESSORY METHODS TO GET empName AND empId;
*/
public String getEmployeeName(){
return this.eName;
}

public String getEmployeeId(){
return this.empNo;
}

private void setEmployeeName(){
Connection connection = null;
try{
connection = connectionManager.getConnection();
PreparedStatement stmt = connection.preparedStatement("Select eName from Emp where empNo = ?");
stmt.setString(1, getEmployeeId());
ResultSet result = stmt.executeQuery();
while(result.next()){
this.eName = result.getString(1);
}
result.close();
stmt.close();
connectionManager.releaseConnection();
}catch(SQLException e){
connectionManager.releaseConnection();
e.printStackTrace();
}
}
}
Thanks in advance for any advice.
Roger
[ October 04, 2002: Message edited by: Roger Garner ]

Roger G<br /> <br />Anybody who tries to make anything idiot proof has obviously underestimated the intelligence of an idiot.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why is this line
public connectionManager = OracleConnection.getInstance;
where it is?
AND should it not be OracleConnection.getInstance()?
AND consider making OracleConnection.getInstance() a static (that is a class) method.
Please tell us what compiler/runtime messages you are getting.
-Barry
[ October 05, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The following compiles, but I have not run it, and I do not take any responsibility for what it does if you try to run it.
Compare it line for line with your original, there
are several significant changes.
Roger Gazdzicki
Ranch Hand

Joined: Oct 04, 2002
Posts: 47
First thanks for the assistance.
I am using textpad to compile the code and Java 1.4. the container will be resin once I get it compiled.
I have errors compiling.
D:\code\new\Emp.java:6: cannot resolve symbol
symbol : class OracleConnection
location: class Emp
public OracleConnection connectionManager = OracleConnection.getInstance();
^
D:\code\new\Emp.java:10: cannot resolve symbol
symbol : class OracleConnection
location: class Emp
public Emp(OracleConnection connectionManager, String empNo){
^
D:\code\new\Emp.java:6: cannot resolve symbol
symbol : variable OracleConnection
location: class Emp
public OracleConnection connectionManager = OracleConnection.getInstance();
^
3 errors
Tool completed with exit code 1
I have put all the code in the same folder hoping that it would find the class.
I moved the .java file out that did not help.
I put the classpath to read "d:\code\new" No help
I am new to connections and singletons. I have read about them and understand what they are and why they are needed, but cannot get it to compile. I dont understand what is wrong.
Again thanks for the help.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I just copied & pasted the code right off this page
and used boring notepad to create Emp.java.
Then I did javac Emp.java and it compiled without
error.
Try the same, just have %JAVA_HOME%\bin in your PATH
where JAVA_HOME is something like c:\jdk1.3.1_02
Do set CLASSPATH= to clear the classpath and it
should compile OK.
Once you have it compiled move it all into your textpad making sure all settings are correct.
-Barry
[ October 06, 2002: Message edited by: Barry Gaunt ]
Roger Gazdzicki
Ranch Hand

Joined: Oct 04, 2002
Posts: 47
YOU ARE RIGHT!!!
I compiled it in dos after clearing my classpath and it worked. Then I restarted textpad and it still worked.
Thanks. I appreciate your help
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Now check all those changes I did to get
the code to compile. Make sure it does what you expect.
Good Luck
-Barry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC Connection