object help

im trying to use the class below as an object like this

Connection conn = DBConnection.getDBConnection();

but doesnt work compiler says DBConnection not found.. both files in same directory

[code]

import java.sql.Connection;

import java.sql.SQLException;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

public class DBConnection extends Object

{

public static Connection getDBConnection() throws SQLException

{

Connection conn = null;

try

{

InitialContext ctx = new InitialContext();

DataSource ds = ( DataSource ) ctx.lookup( "java:comp/env/jdbc/MySQLDB" );

try

{

conn = ds.getConnection();

}

catch ( SQLException sqlEx )

{

System.out.println( "cannot get JDBC connection: " + sqlEx );

}

}

catch ( NamingException nEx )

{

nEx.printStackTrace();

}

return conn;

}

public static void closeConnection(Connection conDB) {

// Return Connection to the pool

try {

conDB.close();

}

catch(SQLException se)

{

// handle any errors

}

// Set object to be ready for garbage collection

conDB = null;

} // end closeConnection

[code]

[1670 byte] By [mo_ali1] at [2007-9-30 2:55:33]
# 1
What do you mean by same directory? is it same package? if they're on the same package i think it should work, maybe it is a god ideia to post the source of both classes.ngaku
ngakumona at 2007-6-29 10:53:57 > top of Java-index,Security,Event Handling...
# 2

i dont know why it aint working! thats the only error i am getting...cant resolve smybol

[code]

package multipleChoice.test;

import java.sql.Connection;

import java.sql.SQLException;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import java.util.logging.*;

public class DBConnection

{

public static Connection getDBConnection() throws SQLException

{

Connection conn = null;

try

{

InitialContext ctx = new InitialContext();

DataSource ds = ( DataSource ) ctx.lookup( "java:comp/env/jdbc/MySQLDB" );

try

{

conn = ds.getConnection();

}

catch ( SQLException sqlEx )

{

System.out.println( "cannot get JDBC connection: " + sqlEx );

}

}

catch ( NamingException nEx )

{

nEx.printStackTrace();

}

return conn;

}

public static void closeConnection(Connection conDB) {

// Return Connection to the pool

try {

conDB.close();

}

catch(SQLException se)

{

// handle any errors

}

// Set object to be ready for garbage collection

conDB = null;

} // end closeConnection

}

[code]

public class addTest

{

Connection conn = DBConnection.getDBConnection();

String error, errorMsg;

int insertedID = -1;

ResultSet rs = null;

public addTest()

{

}

// some methods

mo_ali1 at 2007-6-29 10:53:57 > top of Java-index,Security,Event Handling...
# 3
HEP PLEASE
mo_ali1 at 2007-6-29 10:53:57 > top of Java-index,Security,Event Handling...