JBUILDER9 WebLogic7 actual articles
Application of Entity Bean (2)
Author: Kay Huang
E_mail: hk_sz@163.com
V. Use EJB QL to develop inquiry
5.1 What is EJB QL
The query is often used in the operation of the relational database, mainly through the SELECT statement. Entity Bean also requires query operations as a persistent component representing data in a database, that is, an instance of Entity Bean that meets a query condition can be found. The query operation of the Entity bean is done by defining the finder () method. For CMP, define the Finder () method is merely declaring a method, indicating the parameters of the Finder () method, which is usually corresponding to the parameters in the query condition, and the actuated action is done by the EJB container. The EJB container is to read the
The development query steps of EJB QL are as follows:
1> Increase the Finder () method in the HOME interface, which is the parameters used in the query condition;
2> Define the EJB QL statement in the
EJB QL is the new feature of EJB2.0, which implements how to define various lookup methods in the HOME interface. It is based on SQL-92, which can be automatically compiled by the container, which makes the Entity Bean have higher portability and is easy to deploy.
The EJB QL statement consists of three clauses of SELECT, WHERE, ORDERBY, and thereafter two clauses are optional.
The EJB QL query statement is as follows:
example 1:
Select Stu from student as stu where stu.grade> 5
The meaning of the query statement is to query the Student Bean instance of Grade> 5. "Student" is an abstract mode name (the name specified by the
Example 2:
SELECT I from student as I where I.name =? 1
The meaning of this query statement is the STUDENT bean instance that looks for the first parameter in the Finder () method.
WHERE words instructions:
★ Input parameters of the corresponding Finder () method in? N;
★ The value of the string type should be enclosed in single quotes (if there is a single quota in the value, use the double quotation mark)
The expressions and operators that can be used in the WHERE statement are as follows:
☆ , -, *, /, =, <, <=,> =,>, <>, not, and, or
☆ between
☆ Like
☆ in
☆ MEMBER OF
☆ Is NULL (is not null)
Built-in function:
● Concat (String First, String Second)
● Substring (String Source, Int Start, Int Length)
● Locate (String Source, String Patter) ● Length (String Source)
5.2 Development Example of Query Method
5.2.1 Open the COMSample project established in "Entity Bean (1)". Double-click Cmpsample.jpx / Cmp in the project window, right-click the Student item in the EJB Designer of the Document window, select the Add / Finder menu item, create a Finder () method.
5.2.2 Set various parameters of the Finder () method.
★ Finder Name: FindByname (Finder () method name)
★ Return Type: StudENTREMOTE (Return to the type of object)
★ Input Parameter: java.lang.string name (input parameters)
★ Query: SELECT I from Student As I where I.stuname =? 1 (complete the query EJB QL statement)
5.2.3 Checking Student Entity Bean (Studient HOAVA) will find an additional Finder () method, the method is as follows:
Public Student FindByname (String Name).
5.2.4 Right-click CMPSAMPLE.JPX / CMP in the Project window and select the Rebuild command to re-package.
5.2.5 Start WebLogic Server, then right-click Cmpsample.jpx / Cmp in the Project window, select the Deploy Options for "Cmp.jar" / redeploy command to redeploy.
5.2.6 Creating a client program StudentTClient2.java via EJB Test Client Wizard, because EJB has changed, so you can't use the previously created client program. Modify the main () method is as follows:
Public static void main (string [] args)
{
StudentTestClient2 Client = New StudientTestClient2 ();
Client.studEntremote = Client.FindByname ("student2");
System.out.println ("THE Student2's ID IS" Client.getStuid ());
}
This method looks for the entity bean instance named Student2 through the FindByname () method, and then outputs the STUID value corresponding to the Entity Bean.
5.2.7 Run the client program and verify the result.
For the sake of simplicity, only one parameter is used and returned is a bean instance. If the result of the query is not unique, then set the return value as a collection.
reference
Electronic Industry Press "J2EE App Development (JBuilder WebLogic"
My article is the first Auro Forum (www.newer.com.cn/bbs) and programmers forum (www.9cbs.net), welcome to reprint, but please keep the author and the name of the revision, thank you.