• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JDBC Connection

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now check all those changes I did to get
the code to compile. Make sure it does what you expect.
Good Luck
-Barry
 
Water proof donuts! Eat them while reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic