Practical JBUILDER7 JBOSS3 SQL Server2000
Example of a complete three-layer structure
Liu Xiaowei: Liuxiaowei2000@sina.com
Introduction:
This article tells the implementation of the J2EE for establishing a typical 3-layer structure, mainly to implement the process, no theoretical instructions. This is completely written according to my personal experience, I hope to help everyone, welcome to a lot of criticisms!
Suppose JBuilder7 and JBoss3x are installed, the operating system is: Windows2000 Server (SP2), the database is: SQLServer2000 (SP2). JDBC is the driver of Microsoft SQLServer2000. Example source code: http://weisoft.myrice.com/download/jbossdemo.ra, (SQLServer SA password 9cbs)
One. Environment setting
1. Download and install JBossopentool for jbuilder 7http: //weisoft.myrice.com/download/jbuilder_jbossot.jar copies the downloaded JBossopentool.jar to jBuilder7's lib / ext directory, follow JBuilder.
1. Configure MS SQL Server 2000 JDBCJBOSS settings: Copy msbase.jar, msutil.jar, and mssqlserver.jar three files to JBoss's Server / default / lib directory. Copy the mssql-service.xml of JBoss DOCS / EXAMPLES / JCA directory to the server / default / deploy directory of jboss, and make the following modification: mssql-service.xml (red is The modification section is the database name, user name, password, according to your environmental adjustment):
2. Start JBUILDER7, select: Tools-> configure Servers Select the JBoss 3.x on the left, select the enable server on the right, follow the table information to fill in: General Page: Home Directiry = f: /jboss-3.0.4_tomcat-4.1. 12 // <- JBoss installation directory main class = org.jboss.mainvm parameters = -ms64m -mx64m -dprogram.name = Run.batserver parameters = -c defaultWorking Directory = f: /jboss-3.0.4_tomcat-4.1. 12 / Bincustom Page: JBoss Installation Directory = f: /jboss-3.0.4_tomcat-4.1.12 // <- JBoss installation directory Tomcat 4x jbuilder installation directory = f: /jbuilder7/jakarta-tomcat-4.0.3 // <- Tomcat installation directory jboss configuration to use = default Click OK. 3. Select Tools-> Configure Libraries, click the New button in the pop-up dialog box, fill in the JBoss Libs in the Name column, click the Add button, join the javax.servlet under the / server / default / lib directory in the JBoss directory. JAR and JBOSS-J2EE.JAR files, click OK.
two. Implementation steps
1. Create a new project: Select File-> New Project, fill in JBossDemo in the Name column, enter the save path in the Directory bar, others constant, click Finsih. Choose Project-> Project Properties, click the Add button in the Required Libraries in the Path page, select the JBoss Libs we just created in the pop-up dialog box, click OK. Select Server page, select Single Server for All Service in Project, select JBoss 3.x in the drop-down list, click OK.
2. New EJB Module: Select File-> New, select the EJB Module icon in the Enterprise page in the pop-up dialog box, click OK. In the pop-up dialog box, fill in JBossDemOModule, others unchange, click OK.
3. Newly built two CMP Right-click on Datasource in the lower left corner, select Import Schema from Database, fill in in the pop-up dialog box as follows: driver = com.microsoft.jdbc.sqlserver.sqlserDriverURURL = JDBC: Microsoft: SQLServer: / / localhost: 1433; DatabaseName = NorthwindUserName = sa // ßSQLServer username password = "" // ßSQLServer password JNDI name = java: / MSSQLDS add a DatabaseName = Northwind in the Extended properties in the selected item All Schemas and Views, click OK. Successful, a Java: / MSSQLDS item will appear in DataSource, click the number on the left, will expand, will see the table name. Right-click the Employees table, select CMP 2.0 Entity Bean, and right click on the OrderS table, select CMP 2.0 Entity Bean. 4. Establish a table relationship Right-click on the rectangle of the Employees table on the right, select Add-> Relationship, click to point to the rectangle representative ODERSS table, will see a line to connect two rectangles, while Employees Table has a more ORDERS field. Click the jbosscmp-jdbc page in the lower right corner, fill in the following, other unchanged: DataSource = mssqldsDataSource-mapping = hypersonic SQL Select File-> Save all to save items. Select Project -> "make Project JBossDemo.jpx" compile project.
5. Establish a session bean to right-click on the blank of the right, select Create EJB-> session bean, change bean name to getData, right-click Rectangle on behalf of this session bean, select Add-> Method, fill in: Method Name = getordersbyemployeesidReturn Type = VectorInput Parameters = INTEGER IDINTERFACE = Remote Select File-> Save ALL Save Item. Select Project -> "make Project JBossDemo.jpx" compile project.
6. Write the code Right-click on the rectangle of GetData, select View Bean Source. Add the following statement at the beginning: Import java.util. *; Import javax.naming. *; Fill in getOrDersbyemployeesid as follows:
Public Vector GetOrdersbyemployeesid (Integer ID)
{
Vector result = new vector ();
Result.Add (ID);
Try
{
Context ctx = new initialContext ();
Employeeshome Home = (EMPLOYEESHOME) CTX.lookup ("Employees"); Employees EMP = Home.FindByprimaryKey (ID);
Collection Orders = Emp.getRDERS ();
Iterator it = Orders.iterator ();
While (it.hasnext ())
{
ORDERS Order = (Orders) it.next ();
Result.add (Order.GetOrderId ());
}
}
Catch (Exception EX)
{
System.out.println ("ShowordersByemployeesid () error:" ex.getMessage ();
}
Return Result;
}
7. Establish a servlet
Select File-> New, select the servlet icon in the web page in the pop-up dialog box, click OK. In the pop-up dialog box, fill in the FormServlet, others unchange, click Next, select the doget () item, others constant, click Finish. Fill in FormServlet.java as follows: Public void doget (httpservletRequest Request, HttpservletResponse Response) Throws ServleTexception, IOException
{
Response.setContentType (Content_Type);
PrintWriter out = response.getwriter ();
Out.println ("
Out.println (" Enter Employees ID in The Input Fields Below. Strong>");
out.println ( "
");
Out.println ("