EJB TUTORIAL (recommended)

zhaozj2021-02-11  156

I passed directly, I don't know if there is any deterioration in the format. If it looks not neat, you can go

My blog goes check out :) There is also the download of the source file there.

The environment is set to: WebLogic 6.1 JBuilder 9;

The data table is TMP_EMP, and this table is required to operate in the way in eJB. Field is:

SQL> DESC TMP_EMP;

Name Type Nullable Default Comments

----------------------------------- ---------------------

ID VARCHAR2 (10) Staff ID

Name varchar2 (20) Y staff name

Job varchar2 (20) Y staff's work name

HIREDATE DATE Y staff

Sal Number (7, 2) Y staff salary

Comm varchar2 (20) Y Note

DEPTID VARCHAR2 (10) Y staff sector ID; ID association with the TMP_DEPT table

Preparatory: Encoding Basic Pattern: The package is all lowercase letters, such as the method of combining the EJBSAMPLE.Session class name, and the first letter uppercase, such as the Method of EmployeeAction variable name, and the first letters, such as the Strname module Level variables plus underscores, such as _strname database settings: Install Oracle's client to ensure that the database server has been properly established. JBUILDER9 setting: You can refer to the previous article. Key points: Oracle's library file setting; "Enterprise Setup" Sets WebLogic settings:

Start WebLogic Open Console: http: // localhost: 7001 / Console inside the JDBC's Connection Pools, "Configure a new jdbc connection pool, parameter can refer to: ** Name:

EJBDEMO

(Description: This can be arbitrarily taken)

** URL:

JDBC: Oracle: Thin: @ 172.16.3.100: 1521: DEV3

(Note: This URL is the URL when the JDBC is connected, and the format can refer to the JDBC document; DEV3 is the SID of the database server 172.16.3.100)

** Driver ClassName:

Oracle.jdbc.driver.Oracledriver

** Properties:

User = PM

Password = PM

DLL = OCIJDBC8

Protocol = thin

(Description: Username and password are replaced by oneself)

** Initial Capacity

3

** Maximum Capacity

10

(Note: Don't forget to "Targets" "Available" Apply to "Chosen") Configure a "TX Data Source": ** name:

EJBDS

** JNDI NAME: EJBDS

(Note: This is when the EJB is designed, the data source name to be specified)

** pool name:

EJBDEMO

(Note: This is the name of the previous Connection Pool that creates "EJB Module" in JBuilder: It is recommended to put the save location of the .jar file to the Applications directory of WebLogic: c: /bea/wlserver6.1/config/ Mydomain / Applications

After building an empty Module, first configure its "Datasource". Right click "Datasource" and select "Import Schema from Database". First, "DRIVER" is "Oracle.jdbc.driver.OracleDriver", then the URL fills in "JDBC: Oracle: Thin: @ 172.16.3.100: 1521: dev3", "username" and "Password" fill in the correct (I The "PM", "PM"), "JNDI Name" is filled in "JNDI Name", and after the JBuilder automatically begins "Importing Schema" in the database. If such actions do not have this action, the previous database is not correct, check it again. Designing entity beans in JBuilder's EJB Designer: In the data sheet you have to operate from the JBuilder, find the TMP_EMP table we want to operate. Right click on it, select "CREAT CMP 2.0 Entity Bean", and JBuilder will automatically generate entity beans according to the structure of the table. If you are not satisfied with the parameters generated by JBuilder, you can modify it. I changed as follows: ** bean name:

EBNEMPLOYEE

(Note: After the Bean Name, Abstract Schema Name will automatically change the same name)

** Interfaces:

Local

** Classes and Packages:

Ejbsample.employee.Entity

(Note: a) The package is all lowercase b) On JBuilder's CLASS AND PACKAGES definition page, you only need to enter default package for ejbsample.employe.Entity,

Other Items JBuilder will be filled with)

** Add 3 methods (of course, if you need to add a method, you can always be added to the JBuilder's EJB designer):

Void SetModel Model (Interface: Local)

Employeemodel getModel () method (interface: local)

EJBCREATE (EMPLOYEEMODEL MODEL) Method (Interface: Local Home)

(Description: Employeemodel class is now established)

Design session bean in JBuilder's EJB Designer: Right-click on the EJB design container in JBuilder, select New Session Bean. The modified parameters are as follows: ** bean name: sbnemployee ** session type: stateless

** Interfaces: Remote

** Class - Default Package: EJBSample.employee.Session

** Add the following method (Of course, if you need to add a method, you can be added to the JBuilder's EJB designer to add):

Employeemodel INSERT (EMPLOYEMODEL MODEL) Method (Interfaces: Remote)

Employeemodel Update (Interfaces: remote)

Boolean del (String PK) Method (Interfaces: Remote)

Employeemodel FindBypk (String PK) Method (Interfaces: Remote)

Boolean Delbatch (String [] SID) method (Interfaces: remote)

Java.util.ArrayList QueryBysql (interface strsql) method (Interfaces): Remote

(Description: We can see the use of the Employeemodel class is everywhere. In fact, all pages displaying elements in Employeemodel,

When you deal with the JSP page, it plays an important role. )

Let's start building an Employeemodel class: New Class, named EMPLOYEMODEL, Package Defined as EJBSAMPLE.EMPLOYEEEEEEEEEEEEEEEEE, the base class is Java.io.Serializable. The content is as follows: package ejbsample.employee;

Import java.io.serializable;

/ **

* Title: (no title)

* Description: In general, XXModel classes can be seen as packaging of all display elements for JSP pages. It will be a class that is mainly dealing with the JSP page.

* Copyright: CopyRight (C) 2003

* Company: ebuilds

* @Author Alex

* @version 1.0

* /

Public Class Employeemodel Implements Serializable {

Private string _strid; // staff ID

Private string _strname; // staff name

Private string _strjob; // staff work name

Private java.sql.date _dthired; // staff member

Private Double_dsal; // Staff Salaries

Private string _strcomm; // Remark

Private string _StrDeptid; // Staff is associated with the department ID of the TMP_DEPT table

// Set the property, read the attribute start

Public string getId () {

Return_strid;

}

Public void setid (string in) {_strid = in;

}

Public string getname () {

Return_strname;

}

Public void setname (string in) {

_strname = in;

}

Public string getJob () {

Return_strjob;

}

Public void setjob (string in) {

_STRJOB = IN;

}

Public java.sql.date gethidate () {

Return_dthired;

}

Public void designate (java.sql.date in) {

_dthiredate = in;

}

Public double getsal () {

Return_dsal;

}

Public void setsal (double in) {

_dsal = in;

}

Public string getcomm () {

Return_STRCOMM;

}

Public void setcomm (string in) {

_STRCOMM = IN;

}

Public string getDeptid () {

Return_STRDEPTID;

}

Public void setdeptid (string in) {

_STRDEPTID = IN;

}

// Set the property, read the attribute end

Public void setmodel (Employeemodel IN) {

_strid = in.GetId ();

_StrName = in.getname ();

_STRJOB = in.getjob ();

_dthiredate = in.GethiRedate ();

_dsal = in.getsal ();

_STRCOMM = in.getComm ();

_STRDEPTID = in.getDeptid ();

}

Public employeemodel () {

}

Public static void main (String [] args) {

Employeemodel Employeemodel1 = New Employeemodel ();

}

}

Write a public class of EJB: EJBCOMMON.JAVA. Very important code! Package ejbsample;

/ **

* Title: (no title)

* Description: This is a common class that needs to be used when writing EJB, such as a JNDI name, take the home interface. This class is very necessary when there are many EJBs.

* Copyright: CopyRight (C) 2003

* Company: ebuilds

* @Author Alex

* @version 1.0

* /

Import java.util. *;

Import javax.naming. *;

Import javax.ejb. *;

Import javax.rmi.portableremoteObject;

Import javax.naming.initialcontext;

Import javax.naming.namingexception;

Import Weblogic.jndi.environment;

Import javax.sql. *;

Import java.sql. *;

Public class ejbcommon {

// This is stored all JNDI names. If the JNDi name is much, it is recommended to put them separately in a file. // Description: JNDI name is here: JB "Project Content", double-click "EBNEMPLOYEE", you can see "local home jndi name", that is.

Public static final string e_employee_jndi = "ebnemployee";

Public static final string s_employee_jndi = "sbnemployee";

Public static final string DataSource_jndi = "EJBDS";

/ / The following is a function related to the HOME interface

Private static context context = NULL;

/ **

* Take InitialContext for use after using the home interface

* @Return

* @Throws Namingexception

* /

protected static context getinitialcontext () throws namingexception {

String user = NULL;

String password = NULL;

Properties Properties = NULL;

Try {

Properties = new property ();

Properties.put (Context.Initial_Context_Factory, "WebLogic.jndi.wlinitialContextFactory";

IF (user! = null) {

Properties.Put (Context.security_principal, user);

Properties.put (Context.Security_credentials, Password == Null? "": password);

}

Return New InitialContext (Properties);

}

Catch (namingexception e) {

Throw e;

}

}

/ **

* Used to obtain a remote home interface. In general, the interface of the session bean is remote (not local), so this function is used to get the HOME interface of the session bean.

* @Param lookupname

* @Param Homeclass

* @Return

* /

Public Static Object GetRemoteejbhome (String Lookupname, Class Homeclass) throws namingexception {

Try {

IF (context == null) {

CONTEXT = GetInitialContext ();

}

Object home = portableremoteObject.narrow

Context.lookup (Lookupname),

HomeClass;

Return home;

} catch (namingexception ne) {

// throw new ejbexception (ne.getMessage ());

Throw ne;

}

}

/ **

* Used to obtain a local home interface. In general, the interface of the entity bean is local (LOCAL), so the HOME interface for obtaining the entity bean is used to use this function. * @Param lookupname

* @Return

* /

Public Static Object GetLocaleJbhome (String Lookupname) throws namingexception {

Try {

IF (context == null) {

CONTEXT = GetInitialContext ();

}

Object home = context.lookup (lookupname);

Return home;

} catch (namingexception ne) {

// throw new ejbexception (ne.getMessage ());

Throw ne;

}

}

// The following is the operation of the database. An important thing, after getConnection (), remember to release the connection!

/ **

* Remove a connection from the defined DataSource

* @Return

* /

Public static connection getConnection () throws namingexception, sqlexception {

Connection con = NULL;

Try {

IF (context == null) {

CONTEXT = GetInitialContext ();

}

DataSource Datasource = (Datasource) Context.lookup (DataSource_jndi);

Con = DataSource.getConnection ();

} catch (namingexception ex) {

Throw EX;

} catch (sqlexception ex) {

Throw EX;

}

Return con;

}

/ **

* This is a remote version taken. But it seems that you don't need to use it.

* @Return

* /

Public static connection getConnection_remote () throws namingexception, sqlexception {

Try {

IF (context == null) {

CONTEXT = GetInitialContext ();

}

DataSource DS = (Datasource) PortableRemoteObject.narrow

Context.lookup (datasource_jndi),

DataSource.class;

Return DS.GetConnection ();

} catch (namingexception ne) {

// throw new ejbexception (ne.getMessage ());

Throw ne;

} catch (sqlexception e) {

// throw new ejbexception (E.GetMessage ());

Throw e;

}

}

/ **

* Role: Enter strmsg into the C: / strlogfile file with HTML format

* Will output a statement similar to this:

* [2003-April -29 04:44:03] Debugged file: vwdetail.jsp is called the function: (Start setting up ProjectID) debug description:

Projectid acquired is ERPPM20021211000000000658310000E8E7FF69 *

* Added by Daihua 2003-4-29 11:16

* @Param strmsg

* /

Public Static Void LogoneEtofile (String StrdebugFileName, String strdebugfuncname, string strdebugmsg) {

// If you don't need Debug output, return directly

/ * if (! Debuggingon)

Return;

* /

Java.io.file fd = new java.io.file ("c: // debug");

IF (! fd.exiss ()) {

fd.mkdirs ();

}

/ / Automatically generate DEBUG file names based on the current date, such as: C: / 20030506Debug.html

/ * SimpleDateFormat SDFF = New SimpleDateFormat ("YYYYMMDD");

String strlogfilename = "c: // debug //" SDFF.Format (new date ()) "debug.html";

* /

String strlogfilename = "c: // debug //" strlogfile; / / still specifies the file name of DEBUG by the program

Java.text.SIMPLEDATEFORMAT BARTDATEFORMAT = New java.text.SIMPLEDATEFORMAT ("YYYY-MMMM-DD HH: MM: SS");

Java.util.date Date = new java.util.date ();

Java.io.filewriter out = null;

Try

{

Java.io.file f = new java.io.file (strlogfilename);

IF (! f.exiss ()) {

F.createNewFile ();

}

// out = new randomaccessfile (f, "rw");

OUT = New java.io.filewriter (strlogfilename, true); // to be ready for append

//out.seek (out.Length ());

String str1 = "

" [" bartdateformat.format (date) " ] Debug file: ";

String str2 = "" strDebugfilename "";

String str3 = " Turkey: strDebugfuncname " "; string str4 =" < Font Color = / "# 999999 /"> Debug Description:
/ r / n ";

StrDebugmsg = "" strDebugmsg " / r / n"; strDebugmsg = str1 str2 str3 str4 strDebugmsg;

//out.writechars ("[" bartdateformat.format (date) "]" strmsg "/ r / n");

Out.write (strDebugmsg);

} catch (Exception E)

{

E.PrintStackTrace ();

}

Finally {

Try {

IF (Out! = NULL)

Out.close ();

} catch (exception ex) {

EX.PrintStackTrace ();

}

}

}

Public ejbcommon () {

}

Public static void main (String [] args) {

EJBCOMMON EJBCOMMON1 = New Ejbcommon ();

}

}

Editing Entity Bean

Open EBNEMPLOYEE, EBNemPloyeeHome, EBNemPloyeebean, first imported: import ejbsample.employee.employeemodel; then edit EBNEMPLOYEEBEAN, complete the function of the EJB designer

Void SetModel Model function: public void setmodel (Employeemodel model) {

This.SetId (Model.getID ());

this.setname (Model.getName ());

This.setJob (model.getjob ());

This.SethiRedate (Model.GethiRedate ());

This.Setsal (Model.getsal ());

this.setcomm (model.getComm ());

This.SetDeptid (Model.getDeptid ());

}

Employeemodel getModel () function public Employeemodel getModel () {

Employeemodel Data = New Employeemodel ();

Data.setid (this.getid ());

Data.setname (this.getname ());

Data.setJob (this.getJob ());

Data.sethiRedate (this.GethiRedate ());

Data.setsal (this.getsal ());

Data.setComm (this.getComm ()); Data.SetDeptid (this.getDeptid ());

Return Data;

}

Java.lang.String Ejbcreate (Employeemodel model) public java.lang.string ejbcreate (Employeemodel model) THROWS CRETEEXCEPTION {

// Description: This function is very important. When you create an entity bean, EJBCREATE is automatically called, so if this function is not completed, the entity bean will not be assigned.

SetModel;

Return NULL;

}

Edit the session bean class sbnemployeebean.java, as follows Package Ejbsample.employee.Session;

Import javax.ejb. *;

Import ejbsample.employee.employeemodel;

Import ejbsample.ejbcommon;

Import java.util.arraylist;

Import ejbsample.employee.entity. *;

Import javax.naming.namingexception;

Import java.sql. *;

Import java.rmi. *;

Public class sbnemployeebean imports sessionbean {

SessionContext sessionContext;

Public void ejbcreate () throws createException {

/ ** @ Todo Complete this method * /

}

Public void ejbremove () {

/ ** @ Todo Complete this method * /

}

Public void ejbactivate () {

/ ** @ Todo Complete this method * /

}

Public void ejbpassivate () {

/ ** @ Todo Complete this method * /

}

Public void setsessionContext (sessioncontext sessioncontext) {

This.SessionContext = sessionContext;

}

Public Employeemodel INSERT (Employeemodel Model) throws createException, Namingexception, RemoteException {

Try {

// Use the EJBCOMMON class to get the home interface of the entity bean

EBNEMPLOYEHOME OBJHOME = (EBNEMPLOEEHOME) EJBCOMMON.GETLOCALEJBHOME (EJBCOMMON.E_EMPLOYE_JNDI);

EBNEMPLOYEE Objremote = Objhome.create (model);

Return model;

}

Catch (createException e) {

Throw e;

}

Catch (namingexception e) {

Throw e;

}

}

Public Employeemodel Update (Employeemodel Model) Throws FinderException, Namingexception, RemoteException {

Try {

// Use the EJBCOMMON class to get the home interface of the entity bean

EBNEMPLOYEHOME OBJHOME = (EBNEMPLOEEHOME) EJBCOMMON.GETLOCALEJBHOME (EJBCOMMON.E_EMPLOYEE_JNDI); EBNEMPLOYEE OBJLOCAL = Objhome.FindByPrimaryKey (MODEL.GETID ());

// Call the MODEL's acquisition, update the entity bean

//objlocal.setModel (Model); // can't write this. Specifically, I don't know. It seems that there is no reason.

Objlocal.setname (Model.getName ());

Objlocal.setJob (Model.getJob ());

Objlocal.SethIRedate (Model.GethiRedate ());

Objlocal.setsal (model.getsal ());

Objlocal.setcomm (model.getComm ());

Objlocal.SetDeptid (Model.getDeptid ());

Return model;

}

Catch (FINDEREXCEPTION E) {

Throw e;

}

Catch (namingexception e) {

Throw e;

}

}

Public Boolean Del (String PK) THROWS RemoveException, FINDEREXCEPTION, EJBEXCEPTION, NAMINGEXCEPTION, RemoteException {

Try {

// Use the EJBCOMMON class to get the home interface of the entity bean

EBNEMPLOYEEHOME OBJHOME = (EBNEMPLOEEHOME) EJBCOMMON.GETLOCALEJBHOME

EJBCOMMON.E_EMPLOYEE_JNDI);

EBNemPloyee Objlocal = Objhome.FindByPrimaryKey (PK);

Objlocal.Remove ();

Return True;

}

Catch (RemoveExcect e) {

Throw e;

}

Catch (FINDEREXCEPTION E) {

Throw e;

}

Catch (EJBEXCEPTION E) {

Throw e;

}

Catch (namingexception e) {

Throw e;

}

}

Public Employeemodel FindBypk (String PK) THROWS FINDEREXCEPTION, NAMINGEXCEPTION, RemoteException {

Try {

// Use the EJBCOMMON class to get the home interface of the entity bean

EBNEMPLOYEHOME OBJHOME = (EBNEMPLOEEHOME) EJBCOMMON.GETLOCALEJBHOME (EJBCOMMON.E_EMPLOYE_JNDI);

Employeemodel model = new Employeemodel ();

EBNemPloyee Objlocal = Objhome.FindByPrimaryKey (PK);

Model = Objlocal.getModel ();

Return model;

}

Catch (FINDEREXCEPTION E) {

Throw e;

}

Catch (namingexception e) {

Throw e;

}

}

Public Boolean Delbatch (String [] SID) THROWS NAMINGEXCEPTION, SQLEXCEPTION, RemoteException {

String [] SID = NULL;

SID = SID; STRING SQL = "Delete from TMP_EMP where id = '"; // Note: statmachine is the table name connection with our work

String ID = new string ();

Connection conn = NULL;

Statement ST = NULL;

Boolean b = false;

Try {

CONN = EJBCOMMON.GETCONNECTION ();

Conn.setautocommit (false);

ST = conn.createstatement ();

For (int i = 1; i <= Sid.Length; i ) {

ID = SID [I-1];

St.execute (SQL ID "');

}

CONN.COMMIT ();

B = true;

Return B;

} catch (namingexception e) {

Throw e;

} catch (sqlexception e) {

System.err.Print ("Delbatch () Function Error: Concerning with Database");

Try {

CONN. ROLLBACK ();

} catch (sqlexception sqle) {

Throw sqle;

}

Throw e;

} finally {

Try {

St.close ();

CONN.CLOSE ();

} catch (sqlexception e) {

Throw e;

}

// Return B; // Description: Do not return Return in Finally, otherwise, the captured error will not be throw! ! !

}

}

Public Java.util.Arraylist QueryBysql (String Strsql) Throws Namingexception, Sqlexception, RemoteException {

ArrayList ArrayList = NULL;

Employeemodel item;

Connection conn = NULL;

Statement ST = NULL;

ResultSet RS = NULL;

Try {

CONN = EJBCOMMON.GETCONNECTION ();

ST = conn.createstatement ();

RS = st.executeuery ("Select * from tmp_emp where" strsql); // of course, you can change your condition

System.out.Println ("SQL =" "SELECT * from Tmp_EMP where" strsql);

ArrayList ();

While (rs.next ()) {

item = new employeemodel ();

/ / Read the value from the database

Item.SetId (rs.getstring ("id"));

item.setname (RS.GetString ("name");

Item.setJob (Rs.getstring ("job")); item.sethired (rs.getdate ("hiredate));

Item.setsal (RS.GetDouble ("SAL"));

Item.setComm (Rs.getstring ("Comming");

item.setdeptid (rs.getstring ("deptid");

ArrayList.Add (item);

}

IF (arraylist.size () <= 0) {

Return NULL;

} else {

Return arraylist;

}

} catch (sqlexception e) {

System.out.println ("QueryBysql function has a SQL error:" E.toTRING () ", will return the arraylist set to null");

Throw e;

} catch (namingexception e1) {

Throw E1;

}

Finally {

System.out.println ("QueryBysql function enters Finally sub-block, turn off the connection");

Try {

IF (rs! = null)

Rs.close ();

} catch (sqlexception e) {

Throw e;

}

Try {

IF (st! = null)

St.close ();

} catch (sqlexception e) {

Throw e;

}

Try {

IF (CONN! = NULL)

CONN.CLOSE ();

} catch (sqlexception e) {

Throw e;

}

// Return ArrayList; // Description: Don't return Return in Finally, otherwise, the captured error will not be throw! ! !

}

}

}

Compile EmployeeAction.java. This class wraps EJB and is a class that deals with the JSP page. (This Action class, as well as the previous Model class, is a class that derived two main and JSP pages; and the Action class wrapped the EJB, the Model class wrapped the JSP page display element) New Class, named EMPLOYEAAAction, Package Defined as EJBSAMPLE .employee. The content is as follows: package ejbsample.employee;

/ **

* Title: (no title)

* Description: The XxAction class is basically EJB packaging.

* Copyright: CopyRight (C) 2003

* Company: ebuilds

* @Author Alex

* @version 1.0

* /

Import ejbsample.sequenceutil;

Import ejbsample.ejbcommon;

Import ejbsample.employee.employeemodel;

Import ejbsample.employee.entity. *;

Import ejbsample.employee.Session. *;

Import javax.ejb. *;

Import javax.naming.namingexception;

Import java.rmi.remoteexception;

Import java.util.arraylist;

Import java.sql. *;

Public class employeeaction {/ **

* Incontinent in a Model (no ID this property), automatically add a unique ID, then add a record in the database

* @Param Model

* @Return

* @Throws Namingexception

* @Throws RemoteException

* @Throws CreateException

* /

Public Employeemodel Add (Employeemodel Model) Throws Namingexception, RemoteException,

CreateException {

Model.setId (SequenceUtil.getUnitecode ()); // getUnitecode () The role is to get a unique ID number. How can you write this function, such as using the current time as ID: getTime (); or use the network card physical address plus the current time, etc.

Try {

Sbnemployeehome objhome = (sbnemployeehome) EJBCOMMON.

GetRemoteejbhome (ejbcommon.

S_employee_jndi, sbnemployeehome.class;

Sbnemployee objremote = objhome.create ();

// From the two lines of code you can see the format of the session bean encoded: first get the home interface; then call the create () function to get the interface

Return Objremote.Insert (Model);

}

/ / About error handling: Don't do anything in catch, either you want to turn your catch to the wrong thing, or display some information; otherwise, the program will not debug!

Catch (namingexception e) {

Throw e;

}

Catch (RemoteException E) {

Throw e;

}

Catch (createException e) {

Throw e;

}

}

/ **

* Delete a record inside the database according to the incoming primary key.

* @Param Strpk

* @Return

* @Throws Namingexception

* @Throws RemoteException

* @Throws CreateException

* /

Public Boolean Delete (String Strpk) Throws Namingexception, RemoteException, CreateException, RemoveException, FINDEREXCEPTION {

Try {

Sbnemployeehome objhome = (sbnemployeehome) EJBCOMMON.

GetRemoteejbhome (ejbcommon.

S_employee_jndi, sbnemployeehome.class;

Sbnemployee objremote = objhome.create ();

Boolean B = Objremote.del (STRPK);

Return B;

}

/ / About error handle: Must be such a Catch error, then throw out!

Catch (namingexception e) {

Throw e;

}

Catch (RemoteException E) {

Throw e;

}

Catch (createException e) {

Throw e;

}

Catch (RemoveExcect e) {

Throw e;

}

Catch (FINDEREXCEPTION E) {

Throw e;

}

}

/ **

* Update a record in the database according to the incoming model

* @Param Model

* @Return

* @Throws Namingexception

* @Throws RemoteException

* @Throws CreateException

* /

Public Employeemodel Update (Employeemodel Model) THROWS NAMINGEXCEPTION, RemoteException, CreateException, Finderption

{

Try {

Sbnemployeehome objhome = (sbnemployeehome) EJBCOMMON.

GetRemoteejbhome (ejbcommon.

S_employee_jndi, sbnemployeehome.class;

Sbnemployee objremote = objhome.create ();

Return Objremote.Update (Model);

}

Catch (namingexception e) {

Throw e;

}

Catch (RemoteException E) {

Throw e;

}

Catch (createException e) {

Throw e;

}

Catch (FINDEREXCEPTION E) {

Throw e;

}

}

/ **

* Returns a Model based on the introduced primary key

* @Param Strpk

* @Return

* @Throws Namingexception

* @Throws RemoteException

* @Throws CreateException

* /

Public Employeemodel FindBypk (String Strpk) Throws Namingexception, RemoteException, CreateException, Finderption

{

Try {

Sbnemployeehome objhome = (sbnemployeehome) EJBCOMMON.

GetRemoteejbhome (ejbcommon.

S_employee_jndi, sbnemployeehome.class;

Sbnemployee objremote = objhome.create ();

Return Objremote.FindBypk (STRPK);

}

Catch (namingexception e) {

Throw e;

}

Catch (RemoteException E) {

Throw e;

}

Catch (createException e) {

Throw e;

}

Catch (FINDEREXCEPTION E) {

Throw e;

}

}

/ **

* Depending on the incoming SQL (note is not a complete SQL statement), get all the modes that meet the condition, put it in an arraylist to return

* @Param SQL

* @Return

* @Throws Namingexception

* @Throws RemoteException

* @Throws CreateException

* /

Public ArrayList QueryBysql (String SQL) Throws Namingexception, RemoteException, CreateException, Sqlexception {

Try {

Sbnemployeehome objhome = (sbnemployeehome) EJBCOMMON.

GetRemoteejbhome (ejbcommon.

S_employee_jndi, sbnemployeehome.class;

Sbnemployee objremote = objhome.create ();

Return Objremote.QueryBysql (SQL);

}

Catch (namingexception e) {

Throw e;

}

Catch (RemoteException E) {

Throw e;

}

Catch (createException e) {

Throw e;

}

Catch (SQLException E) {

Throw e;

}

}

/ **

* According to the incoming primary key array, bulk deletion record

* @Param SID

* @Return

* @Throws Namingexception

* @Throws RemoteException

* @Throws CreateException

* /

Public Boolean Deletebatch (String [] SID) THROWS NAMINGEXCEPTION, RemoteException, CreateException {

Boolean b = false;

Try {

Sbnemployeehome objhome = (sbnemployeehome) EJBCOMMON.

GetRemoteejbhome (ejbcommon.

S_employee_jndi, sbnemployeehome.class;

Sbnemployee objremote = objhome.create ();

B = Objremote.delbatch (SID);

}

Catch (namingexception e) {

Throw e;

}

Catch (RemoteException E) {

Throw e;

}

Catch (createException e) {

Throw e;

}

Finally {

Return B;

}

}

Public employeeaction () {

}

Public static void main (String [] args) {

Employeeaction action = new EmployeeAction ();

// The following is the debug code. Note: It is best to debug all the methods in the session bean before the EJB released to WebLogic.

/ / Make sure there will be no lower errors. Otherwise, if you go to the JSP page, then debug it, it is undoubtedly nightmare.

Employeemodel Data = New Employeemodel ();

// Description: 1) If you want to debug, as long as you add a "test XX method" and you can.

// 2) Sometimes the EJB runs slower, and there is no reaction in JBuilder for a long time. This is possible to add some in front.

// Output statement, let you know that the program is operating correctly.

// * Test Add () method

For (int i = 0; i <6; i ) {

Data.setname ("Name" i);

Data.setJob ("job1"); data.sethired (java.sql.date.valueof ("2003-07-21");

Try {

Action.Add (data);

}

Catch (Exception E) {

E.PrintStackTrace ();

}

}

// ** /

/ * Test delete () method

Try {

Action.delete ("ID_2003072504cb077b81059114950140"); // The value of this primary key is found in the database.

}

Catch (Exception E) {

E.PrintStackTrace ();

}

// * /

/ * Test deletebatch () method

String [] string = {"ID_2003072504CB0777B81059116383421", "ID_2003072504CB0777B81059116474625", "ID_2003072504CB077B81059116477218"}

Try {

Action.deletebatch (strsid);

}

Catch (Exception E) {

E.PrintStackTrace ();

}

// * /

/ * Test FindBypk () method

String strpk = "ID_2003072504cb077b8105911647,328";

Try {

Data = action.findBypk (STRPK);

System.out.println ("Name =" Data.GetName ());

}

Catch (Exception E) {

E.PrintStackTrace ();

}

// * /

/ * Test queryBysql () method

String strsql = "JOB1 = 'JOB1'";

ArrayList Alret = NULL;

Try {

Alret = action.queryBysql (strsql);

IF (Alret! = null && alreet ()> 0) {

For (int i = 0; i

Data = (Employeemodel) AlRet.get (i);

System.out.println ("Find the requirements of the requirements 1) " record, name = " data.getname ());

}

} else {

System.out.println ("No Record");

}

}

Catch (Exception E) {

E.PrintStackTrace ();

}

// * /

/ * Debug Update () function

String strpk1 = "id_2003072504cb077b81059118072859"

Try {

Data = action.findBypk (STRPK1);

System.out.println ("Found Old Name =" Data.GetName ());

Data.setname ("new name");

Action.Update (data);

}

Catch (Exception E) {

E.PrintStackTrace ();

}

// * /

System.out.println ("********* final!");

}

Debug EJB: The debugging method is to run the EMPLOYEEAction. Note Before running, you must start WebLogic and ensure that EJB has been published correctly. In fact, the release of EJB should also be commissioned for a long time. Debugging precautions:

Debugging EJB is very troublesome, and it takes a lot of memory. If the memory is not more than 512M or more, it will wait for the pain. Sometimes, all steps are correct, but there are always some strange errors. (For example, when I wrote this example), General ShutDown WebLogic, then remove all temporary directories in the WebLogic directory (generally TMP start), then restart WebLogic. The problem is often able to get resolved. Don't ask why, it is estimated that BEA people don't know. In JBuilder, it is best to remove EJB Module's "Always Creating Jar When Building the Project". So you have to repeat EJB .jar files each time. Sometimes it is compiled. JAR file is large (such as 1M, 2M; general dozens, hundreds of KB is more normal), you have to see if the property settings of EJB Module are correct. Mainly whether Content contains all classes, which generally should choose only the categories that are included. When publishing with JBuilder, sometimes it may encounter a prompt that "more than 4 minutes, time timeout, not success", etc. But it turns out that it is successful, if you encounter this situation, it is ignored. FAQ:

转载请注明原文地址:https://www.9cbs.com/read-5930.html

New Post(0)