Hi all,

I'm new to J2EE programming. I have a simple doubt .

I have already created a lookup method for EJB bean in Session bean .

My question is how to call a method of an ENTITY bean (say insertRow) from SESSION bean method(Say invoke_insertRow) .

Please provide me an example code .

Thanks in advance.

Recommended Answers

All 4 Replies

You don't.
You create a new entity, and it takes care of its database access itself.

You don't.
You create a new entity, and it takes care of its database access itself.

Hi Jwenting ,
First of all thanks for your reply . I think i didn't correctly specified what i need .

Task i need to accomplish :
=================
I have to read ACCESS tables and EXCEL sheets to convert in to XML file so that i can store that values in SQL SERVER using JOnAS Application Server .

Here's my story which will tell what i need :
===========================

I created SESSION BEAN program which contains function which will convert ACCESS/EXCEL file in to XML .

I also created ENTITY BEAN program which contains function to store the data in SQL SERVER(the data which are obtained from that XML file).

i have developed that ENTITY module in such a way that i need to pass the data as arguments of create method of that ENTITY BEAN as follows .

public String ejbCreate(String empid, String firstname, String lastname, String circle, String subcircle, String created_date, String updated_date) throws CreateException {
        try{
            insertRow(empid,firstname,lastname,circle,subcircle,created_date,updated_date);
        }catch(SQLException ex){
            throw new EJBException("ejbCreate: " + ex.getMessage());
        }
        this.empid = empid;
        this.firstName = firstname;
        this.lastName = lastname;
        this.Circle = circle;
        this.Subcircle=subcircle;
        this.Created_Date=created_date;
        this.Updated_Date=updated_date;
        return empid;
        
    }

private void insertRow(String empid, String firstName,
            String lastName, String Circle,String Subcirle,String Created_Date,String Updated_Date) throws SQLException{
        makeConnection();
        String insertStatement = "insert into Employee values ( ? , ? , ? , ?,?, ?,? )";
        PreparedStatement prepStmt = con.prepareStatement(insertStatement);
        
        prepStmt.setString(1, empid);
        prepStmt.setString(2, firstName);
        prepStmt.setString(3, lastName);
        prepStmt.setString(4, Circle);
        prepStmt.setString(5, Subcircle);
        prepStmt.setString(6,Created_Date);
        prepStmt.setString(7,Updated_Date);
        
        
        prepStmt.executeUpdate();
        prepStmt.close();
        releaseConnection();
    }

What i planned to fetch XML data:
=====================

I will develop a function to parse XML file in that SESSION BEAN ( which created that XML file) to obtain that data and i have to pass that values to the above function which is in ENTITY BEAN.

I came to know that i can't invoke create method of ENTITY bean so i need to invoke that insertRow function directly (which actually stores data in SQL SERVER) .

So please guide me HOW CAN I INVOKE a ENTITY BEAN method (insertRow) from an SESSION BEAN with sample coding .

I think it's quite a big story .Thanks for your patience ;)

Thanks in advance.

You do NEVER call those methods directly, how often do I have to repeat that?

In fact, if you don't understand that you can't even call a private method from another class (or even another instance of the same class) you should not be doing any J2EE stuff AT ALL but start with an introductory tutorial on Java.

You should also do some introductory tutorial on EJBs after that, and one that handles MODERN EJBs, as directly doing JDBC access from entity beans is NOT the way to do things, and hasn't been for years.

Hi Jwenting ,
Sorry for such a silly question .

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.