I have a working connection with mysql database:
public static javax.sql.DataSource getJDBCDataSource() throws DAOException
{
try {
if (_ds == null)
{
_ds = (javax.sql.DataSource) getContext().lookup(
CommonUtilities.getServerProperties().getProperty( PropertyFileConfigurationKeys.CONFIG_SERVER_MYSQL_JNDI.toString()
)
);
}
return _ds;
} catch (NamingException ex) {
throw new DAOException("Problem looking up resource by name; see chained exception.", ex);
}
}
The JNDI for the mySQL database is defined in persistence.xml. It is giving a nullpointer exception when am trying to run a query for testing:
try{
DataSource ds = BeanUtilities.getJDBCDataSource();
InterviewerProperty ip1 = new InterviewerProperty(ds);
Collection<EmployeeVO> results = ip1.getEmployees();
for(EmployeeVO next:results) {
System.out.println(next);
}
}
catch(DAOException e){
System.out.println("Exception occured");
System.out.println(e);
}
}
The method getEmployees accessing the database is:
public ArrayList<EmployeeVO> getEmployees() throws DAOException {
Connection connection = null;
PreparedStatement statement = null;
ArrayList<EmployeeVO> result = new ArrayList<EmployeeVO>();
try {
connection = getConnection();
statement = connection.prepareStatement("select * from employee");
ResultSet rs = statement.executeQuery();
while(rs.next()) {
result.add((EmployeeVO) rs);}
rs.close();
} catch (SQLException ex) {
String err = "Unable to fetch Employees";
throw new DAOException(err,ex);
} finally {
close(statement,connection);
}
return result;
}
The error is:
**Exception in thread "main" java.lang.NullPointerException
at javax.naming.InitialContext.getURLScheme(InitialContext.java:269)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:318)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at edu.ufl.bebr.scheduler.framework.BeanUtilities.getJDBCDataSource(BeanUtilities.java:57)
at edu.ufl.bebr.scheduler.ejb3.session.TestRun.main(TestRun.java:44)**
Any help regarding how to fix the error would be great. Thanks in advance!