Database access
alcek Jan 25, 2002 8:01 AMHello to every body.
I'm just learning something about EJB's and I have had my first test with a Session Bean that access directly to a database, the jboss.jcml entry for the Oracle 8i database is:
oracle.jdbc.driver.OracleDriver
OracleDB
org.opentools.minerva.jdbc.xa.wrapper.XADataSourceImpl
jdbc:oracle:thin:@virtualc1:1521:mtst8i
scott
tiger
The run.bat result
[JdbcProvider] Initializing
[JdbcProvider] Loaded JDBC-driver:oracle.jdbc.driver.OracleDriver
[JdbcProvider] Initialized
My META-INF/ejb.jar.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ejb-jar>
Seleccio Taula Emp
<display-name>SeleccioEmp</display-name>
<enterprise-beans>
<ejb-name>SeleccioEmp</ejb-name>
SelectEmpHome
SelectEmp
<ejb-class>SelectEmpBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</enterprise-beans>
</ejb-jar>
This is the remote interface:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface SelectEmp extends EJBObject {
public int recuperarEmp() throws RemoteException;
}
The home interface
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface SelectEmpHome extends EJBHome {
SelectEmp create() throws RemoteException, CreateException;
}
The Bean
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
public class SelectEmpBean implements SessionBean {
public static String recuperarEmp(){
System.out.println("Antes de asignar valor a la variable");
int result = 0;
try{
InitialContext ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDS") ;
java.sql.Connection conn = ds.getConnection() ;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT EMPNO FROM EMP WHERE EMPNO = 37");
rs.next();
result = rs.getInt("EMPNO");
rs.close();
stmt.close();
} catch(Exception e) {System.out.println(e.toString());}
return result;
}
public SelectEmpBean() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
The caller client program:
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
class ClientSeleccioEmp
{
public static void main(String[] args)
{
try
{
Properties env = new Properties();
env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.provider.url", "brahms:1099");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext jndiContext = new InitialContext(env);
System.out.println("Contexto Disponible");
Object ref = jndiContext.lookup("SeleccioEmp");
System.out.println("Se encontro Referencia del EJB!");
SelectEmpHome home = (SelectEmpHome) PortableRemoteObject.narrow (ref, SelectEmpHome.class);
SelectEmp empno = home.create();
System.out.println(empno.recuperarEmp());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
The screen output:
javax.naming.NameNotFoundException: jdbc not bound
I have seen a topic that that looks like, but there someone talk about two files (jboss.xml & jboss-web.xml), I have searching in every where of JBoss Home, but I'm not able to find it. I didn't find where they must to be in the JBoss Administration Book. Please I need some help.