cancel
Showing results for 
Search instead for 
Did you mean: 

Oracle DB connectivity using EJB.

Former Member
0 Kudos

Hii all,

I am making one EJB Application in which i Have to retrieve the data from the Oracle DataBase , I have Done the confiugration of Datasource(Created new one

)in J2EE engine.

When i m running my application it is not givivng me the output data and its not giving any error.

I m giving the copy of code below.

JavaBean Class.

package com.sap.examples.associate.beans;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import com.sap.examples.associate.Associate;

import com.sap.examples.associate.AssociateHome;

/**

  • @author Administrator

*

  • To change the template for this generated type comment go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

public class AssoProxy {

private Associate asso;

public void init() throws Exception {

// Lookup the enterprise bean

try {

InitialContext ctx = new InitialContext();

Object ob = ctx.lookup("java:comp/env/ejb/AssociateBean");

AssociateHome home = ( AssociateHome ) PortableRemoteObject.narrow( ob, AssociateHome.class );

// Initialize the enterprise bean

asso = home.create();

} catch ( Exception e ) {

throw new Exception("Error instantiating Associate EJB" + e.toString());

}

}

public AssoProxy() throws Exception {

init();

}

public int getResult( String firstNumber,String expression ) throws Exception {

int result = 0;

try

{ if ( firstNumber != null ) {

int first = Integer.parseInt(firstNumber);

//int first = Int.firstNumber ;

//float second = Float.parseFloat( secondNumber );

int expr = Integer.parseInt( expression );

// Invoke the relevant method of the enterprise bean

switch ( expr ) {

case 1:

result = asso.deptid( first );

break;

}

}

}catch (Exception re){

throw new Exception("Fill in all required fields with appropriate values!");

}

// Return the result of the calculation

return result;

}

}

**********************

package com.sap.examples.associate;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import javax.ejb.CreateException;

import java.io.*;

import java.sql.*;

import java.util.*;

import javax.sql.DataSource;

import java.util.ArrayList;

import java.util.Iterator;

/**

  • @ejbHome <{com.sap.examples.associate.AssociateHome}>

  • @ejbLocal <{com.sap.examples.associate.AssociateLocal}>

  • @ejbLocalHome <{com.sap.examples.associate.AssociateLocalHome}>

  • @ejbRemote <{com.sap.examples.associate.Associate}>

  • @stateless

  • @transactionType Container

*/

public class AssociateBean implements SessionBean {

public int deptId;

private static Connection con = null;

public static Connection createConnections() throws Exception{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection(

"jdbc:oracle:thin:@psi-pc0067:1527:dsn name ", "username ", "password");

//con = DriverManager.getConnection("jdbc:odbc:dns","scott","tiger");

//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@Oracle_server_ip:Oracle port:SID of the Database","user_name","password");

if(con!=null)

{

System.out.println("connection established");

}

else

{

System.out.println("connection not established");

}

return con;

}

public void ejbRemove() {

}

public void ejbActivate() {

}

public void ejbPassivate() {

}

public void setSessionContext(SessionContext context) {

myContext = context;

}

private SessionContext myContext;

/**

  • Business Method.

*/

public int deptid(int f1) {

//con=AssociateBean.createConnections();

String query="";

//ArrayList emplist=new ArrayList();

Statement st;

ResultSet rs;

query="select deptid from associate where associateid="f1"";

try{

st=con.createStatement();

rs=st.executeQuery(query);

while (rs.next()){

deptId=rs.getInt(1);

}

rs.close();

st.close();

}

catch(Exception e){}

System.out.println("test");

return deptId;

// TODO : Implement

}

/**

  • Create Method.

*/

public void ejbCreate() throws CreateException {

// TODO : Implement

}

}

can anyone tell me what can be probable reason.

thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Devendrakumar,

first of all you should think about the design of your J2EE application. For all entities in your database you can implement a entity bean. There are two posibilities: BMP (Bean-Managed-Persistence, in this case you have to develop all SQLs by your own) or CMP (Container-Managed-Persistence, all DB stuff is done by the container, but you need a OpenSQL Datasource configured in Visual Admin).

If you need a connection to your database, which is configured as datasource, never connect manually to it. You can lookup the connection via JNDI. So the connection is taken from a connection pool, managed by the container.

I hope this helps you. You can find a lot of tutorials about J2EE on the web.

Regards

Sebastian

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

First create one Library project,which conatains all suppoted jar files for oracle database connection. Then deploy into J2EE server after add jars into your project from library project. Then deploy your project.

Kind Regards,

S.Saravanan.

Former Member
0 Kudos

Hi,

it is not needed to deploy the oracle jdbc driver via library project. If the database is configured as datasource in the container, the drivers are already present. Please use JNDI to retrieve the connection, don´t use the Class.forName stuff.

Regards

Sebastian

Former Member
0 Kudos

hii Sebastian,

I have made the datasource in the J2EE engine, but i don't know how to use datasource to retrieve the data from the oracle DB. I have checked the query also it is also working fine and the code what i have give above is also showing a jsp page to take "associateid" value but it is not showing the output i.e deptid.

can u please let me know in which class i have to write code for datasource, query and all.I shallbe thankful to u if u can give me some example or tutorial regarding this.

thanks in advance.

Devendra

Former Member
0 Kudos

Hi,

Better you have to use seperate Class contains one method. It should return connection object. Inside method use the following code to get connection from JNDI.

InitialContext initialContext = new InitialContext();

DataSource dataSource = (DataSource)initialContext.lookup("<JNDI Name>");

java.sql.Connection conn = dataSource.getConnection();

Kind Regards,

S.Saravanan.