Skip to Content
0
Former Member
Jul 31, 2006 at 07:27 AM

Oracle DB connectivity using EJB.

65 Views

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