File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in bmp entity bean while running client?

 
vikram veera
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai everyone,
I have a doubt in setting a envirnoment propery for bmp entity bean.Ie if i want to set the connectivity to the database through environment property it through exception when client run...
If i set the database connectvity with in the bean it works perfectly...
Please help me how to set environment property...
I am using weblogic server5.1 and database is oracle8...
See the program if i set the database connectvity with in the bean
it is okey..
the program is given below

import java.io.Serializable;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
import java.sql.*;
import javax.naming.*;
public class AccountBean implements EntityBean
{
protected EntityContext ctx;
public String accountID;
public String ownerName;
public double balance;

public AccountBean()
{
}
public void deposit(double amount)
{
balance += amount;
}
public void withdraw(double amount) throws AccountException
{
if(amount > balance)
{
throw new AccountException("Request to withdraw $" + amount +
" more than balance " +
balance +
" in account " + accountID);
}
balance -= amount;
// return balance;
}
public double getBalance()
{
return balance;
}

public String getAccountID()
{
return accountID;
}
public void setAccountID(String id)
{
this.accountID=id;
}

public String getOwnerName()
{
return ownerName;
}
public void setOwnerName(String name)
{
ownerName = name;
}
public void ejbActivate() throws RemoteException
{
}
public void ejbRemove() throws RemoteException
{
AccountPK pk =(AccountPK)ctx.getPrimaryKey();
String id = pk.accountID;
Connection con = null;
PreparedStatement ps = null;
try
{
con = getConnection();
ps= con.prepareStatement("delete from accounts where id = ?");
ps.setString(1,id);
int i = ps.executeUpdate();
if (i == 0)
{
throw new RemoteException ("Account ( "+pk+ " ) failed to
remove database");
}
}
catch (SQLException sqe) {
throw new RemoteException (sqe.getMessage());
}
finally
{
try
{
ps.close();
con.close();
}
catch (Exception e)
{
throw new RemoteException (e.getMessage());
}
}
}
public void ejbPassivate()
{
}
public void ejbLoad() throws RemoteException
{
AccountPK pk =(AccountPK)ctx.getPrimaryKey();
String id=pk.accountID;
Connection con = null;
PreparedStatement ps = null;
try
{
con = getConnection();
ps= con.prepareStatement("select ownerName,balance from accounts
where id = ?");
ps.setString(1,id);
ResultSet rs=ps.executeQuery();
rs.next();
ownerName = rs.getString("ownerName");
balance = rs.getDouble("balance");
}
catch (SQLException sqe)
{
throw new RemoteException ("Account"+pk+"failed to load from
database"+sqe);
}
finally
{
try
{
ps.close();
con.close();
}
catch (Exception e)
{
throw new RemoteException (e.getMessage());
}
}
}
public void ejbStore() throws RemoteException
{
Connection con = null;
PreparedStatement ps = null;
try
{
con = getConnection();
ps = con.prepareStatement("update accounts set ownerName =
?,balance = ? where id = ?");
ps.setString(1,ownerName);
ps.setDouble(2,balance);
ps.setString(3,accountID);
ps.executeUpdate();
}
catch (SQLException sqe)
{
throw new RemoteException ("Account"+accountID+"failed to load
from database"+sqe);
}
finally
{
try
{
ps.close();
con.close();
}
catch (Exception e)
{
throw new RemoteException (e.getMessage());
}
}
}
public void setEntityContext(EntityContext ctx) throws
RemoteException
{
this.ctx = ctx;
//env = ctx.getEnvironment();
}
public void unsetEntityContext() throws RemoteException
{
this.ctx = null;
//this.env = null;
}
public void ejbPostCreate(String accountId, String ownerName) throws
RemoteException
{
}
public AccountPK ejbCreate(String accountId, String ownerName) throws
CreateException, RemoteException
{
Connection con = null;
PreparedStatement ps = null;
try
{
this.accountID = accountId;
this.ownerName = ownerName;
this.balance=0;
con = getConnection();
ps = con.prepareStatement("insert into accounts
(ID,ownerName,balance) values(?,?,?)");
ps.setString(1, accountId);
ps.setString(2, ownerName);
ps.setDouble(3, balance);
ps.executeUpdate();
return new AccountPK(accountID);
}
catch (SQLException sqe) {
throw new CreateException (sqe.getMessage());
}
finally {
try {
ps.close();
con.close();
}
catch (Exception e) {
throw new RemoteException (e.getMessage());
}
}
}
public AccountPK ejbFindByPrimaryKey(AccountPK pk) throws
FinderException, RemoteException
{
Connection con = null;
PreparedStatement ps = null;
try
{
con=getConnection();
ps = con.prepareStatement("select id from accounts where id =
?");
ps.setString(1,pk.toString());
ResultSet rs = ps.executeQuery();
rs.next();
return pk;
}
catch (Exception e)
{
throw new FinderException(e.toString());
}
finally
{
try
{
ps.close();
con.close();
}
catch (Exception e)
{
throw new RemoteException (e.getMessage());
}
}
}
public Enumeration ejbFindByOwnerName(String Name) throws
FinderException, RemoteException
{
PreparedStatement ps = null;
Connection con = null;
Vector v = new Vector();
try
{
con=getConnection();
ps = con.prepareStatement("select id from accounts where
ownerName = ?");
ps.setString(1,Name);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
String id = rs.getString("id");
v.addElement(new AccountPK(id));

}
return v.elements();
}
catch (Exception e)
{
throw new FinderException(e.toString());
}
finally
{
try
{
ps.close();
con.close();
}
catch (Exception e)
{
throw new RemoteException (e.getMessage());
}
}
}
//public static final String JDBC_URL="JDBC_URL";
public Connection getConnection() throws SQLException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ee) {}
//String jdbc_URL=(String)env.get(JDBC_URL);
return DriverManager.getConnection("jdbc dbc:sample","scott","tiger");
}
}
this works fine...
------------------------------------------------------------------
If i set this database connectvity through environment property it through exception...
The exception is throws when i run client...
But in deployment it gives no problem..
the program is given below..
import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;
import java.rmi.RemoteException;
public class AccountBean implements EntityBean {
protected EntityContext ctx;
// Bean-managed state fields
public String accountID;// PK
public String ownerName;
public double balance;
// Environment properties the bean was deployed with
public Properties env;
public AccountBean() {
System.out.println("New Bank Account Entity Bean Java
Object created by EJB Container.");
}
//
// Business Logic Methods
//
/**
* Deposits amt into account.
*/
public void deposit(double amt) throws AccountException {
System.out.println("deposit(" + amt + ") called.");

balance += amt;
}

public void withdraw(double amt) throws AccountException {
System.out.println("withdraw(" + amt + ") called.");
if (amt > balance) {
throw new AccountException("Your balance is " +
balance + "! You cannot withdraw " + amt + "!");
}
balance -= amt;
}
public double getBalance() {
System.out.println("getBalance() called.");
return balance;
}
public void setOwnerName(String name) {
System.out.println("setOwnerName() called.");
ownerName = name;
}

public String getOwnerName() {
System.out.println("getOwnerName() called.");
return ownerName;
}

public String getAccountID() {
System.out.println("getAccountID() called.");
return accountID;
}

public void setAccountID(String id) {
System.out.println("setAccountID() called.");
this.accountID = id;
}


public void ejbActivate() throws RemoteException {
System.out.println("ejbActivate() called.");
}

public void ejbRemove() throws RemoteException {
System.out.println("ejbRemove() called.");

AccountPK pk = (AccountPK) ctx.getPrimaryKey();
String id = pk.accountID;
PreparedStatement pstmt = null;
Connection conn = null;
try {

conn = getConnection();

pstmt = conn.prepareStatement("delete from
accounts where id = ?");
pstmt.setString(1, id);


if (pstmt.executeUpdate() == 0) {
throw new RemoteException("Account " +
pk + " failed to be removed from the database");
}
}
catch (SQLException ex) {
throw new RemoteException(ex.toString());
}
finally {
try {
if (pstmt != null) pstmt.close();
}
catch (Exception e) { }
try {
if (conn != null) conn.close();
}
catch (Exception e) { }
}
}

public void ejbPassivate() throws RemoteException {
System.out.println("ejbPassivate () called.");
}

public void ejbLoad() throws RemoteException {
System.out.println("ejbLoad() called.");

AccountPK pk = (AccountPK) ctx.getPrimaryKey();
String id = pk.accountID;
PreparedStatement pstmt = null;
Connection conn = null;
try {

conn = getConnection();

pstmt = conn.prepareStatement("select
ownerName, balance from accounts where id = ?");
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery();
rs.next();
ownerName = rs.getString("ownerName");
balance = rs.getDouble("balance");
}
catch (SQLException ex) {
throw new RemoteException("Account " + pk + "
failed to load from database", ex);
}
finally {

try {
if (pstmt != null) pstmt.close();
}
catch (Exception e) { }
try {
if (conn != null) conn.close();
}
catch (Exception e) { }
}
}

public void ejbStore() throws RemoteException {
System.out.println("ejbStore() called.");
PreparedStatement pstmt = null;
Connection conn = null;
try {

conn = getConnection();

pstmt = conn.prepareStatement("update accounts
set ownerName = ?, balance = ? where id = ?");
pstmt.setString(1, ownerName);
pstmt.setDouble(2, balance);
pstmt.setString(3, accountID);
pstmt.executeUpdate();
}
catch (SQLException ex) {
throw new RemoteException("Account " +
accountID + " failed to save to database", ex);
}
finally {
try {
if (pstmt != null) pstmt.close();
}
catch (Exception e) { }
try {
if (conn != null) conn.close();
}
catch (Exception e) { }
}
}

public void setEntityContext(EntityContext ctx) throws
RemoteException {
System.out.println("setEntityContext called");
this.ctx = ctx;
env = ctx.getEnvironment();
}

public void unsetEntityContext() throws RemoteException {
System.out.println("unsetEntityContext called");
this.ctx = null;
this.env = null;
}
public void ejbPostCreate(String accountID, String ownerName)
throws RemoteException {
}

public AccountPK ejbCreate(String accountID, String ownerName)
throws CreateException, RemoteException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
System.out.println("ejbCreate() called.");
this.accountID = accountID;
this.ownerName = ownerName;
this.balance = 0;

conn = getConnection();


pstmt = conn.prepareStatement("insert into accounts
(id, ownerName, balance) values (?, ?, ?)");
pstmt.setString(1, accountID);
pstmt.setString(2, ownerName);
pstmt.setDouble(3, balance);
pstmt.executeUpdate();

return new AccountPK(accountID);
}
catch (Exception e) {
throw new CreateException(e.toString());
}
finally {

try {
pstmt.close();
}
catch (Exception e) { }
try {
conn.close();
}
catch (Exception e) { }
}
}

public AccountPK ejbFindByPrimaryKey(AccountPK key) throws
FinderException, RemoteException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
System.out.println("ejbFindByPrimaryKey(" + key + ")
called");

conn = getConnection();


pstmt = conn.prepareStatement("select id from accounts
where id = ?");
pstmt.setString(1, key.toString());
ResultSet rs = pstmt.executeQuery();
rs.next();


return key;
}
catch (Exception e) {
throw new FinderException(e.toString());
}
finally {

try {
pstmt.close();
}
catch (Exception e) { }
try {
conn.close();
}
catch (Exception e) { }
}
}

public Enumeration ejbFindByOwnerName(String name) throws
FinderException, RemoteException {
PreparedStatement pstmt = null;
Connection conn = null;
Vector v = new Vector();
try {
System.out.println("ejbFindByOwnerName(" + name + ")
called");


conn = getConnection();


pstmt = conn.prepareStatement("select id from accounts
where ownerName = ?");
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
/*
* Insert every primary key found into a vector
*/
while (rs.next()) {
String id = rs.getString("id");
v.addElement(new AccountPK(id));
}

/*
* Return an enumeration of found primary keys
*/
return v.elements();
}
catch (Exception e) {
throw new FinderException(e.toString());
}
finally {
/*
* Release DB Connection for other beans
*/
try {
pstmt.close();
}
catch (Exception e) { }
try {
conn.close();
}
catch (Exception e) { }
}
}
public static final String JDBC_URL = "JDBC_URL";


public Connection getConnection() throws SQLException {
String jdbcURL = (String) env.get(JDBC_URL);



return DriverManager.getConnection(jdbcURL, env);
}

}

and the environment setting is
Environment property
setting value
-------------------- ----------------------------
jdbc.drivers"sun.jdbc.odbc.JdbcOdbcDriver"
JDBC_URL"jdbc dbc:sample"
It throws a exception like this...
javax.ejb.CreateException: java.lang.NullPointerException
at
weblogic.rmi.extensions.AbstractRequest.sendReceive(AbstractRequest.jav
a:76)
at
AccountBeanHomeImpl_WLStub.create(AccountBeanHomeImpl_WLStub.java:167)
at AccountBeanHomeImpl_ServiceStub.create(Compiled Code)
at Client.main(Client.java:22)
Please help me in this ...
Please send me the steps to solve it...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic