Use Ajax + J2EE to develop the organization management system 5

Source: Internet
Author: User
Vi. Implementation of Background Data

1. data structure definition

Here, we mainly have three tables. One is the organizational structure table, the other is the person table, and the other is the orgperson table. The organizational structure table has orgcodeCode), Orgname (organization name), orgid (Organization ID), and parentorgid (parent ID ). The personnel table contains personcode, personname, sex, and personid ). The orgperson table has orgid and personid.

2. Database Connection

Web ApplicationsProgramMySQL is often used as the background database because MySQL is simple and efficient. Here we also use MySQL as the database. Use JDBC to connect to the database in Java. The code for connecting to the database is as follows:

Public static connection getconnectionfrompool () throws exception {
Context CTX = new initialcontext ();
Datasource DS = (datasource) CTX. Lookup ("Java:/ertp ");
Return Ds. getconnection ();
}

/**
* Retrieve the database link object
* @ Return connection database link object
* @ Throws exception
*/
/*
Public static connection getdirectconnection () throws exception {
Class. forname ("com. Sybase. jdbc2.jdbc. sybdriver ");
String url = "JDBC: Sybase: TDS: 19.64.13.16: 4100/wydb? Charset = iso_1 ";
String user = "sa ";
String Password = "2860008 ";
Connection conn = drivermanager. getconnection (URL, user, password );
Return conn;
}
*/

3. Implementation of the business logic layer

We use Java classes for background development. Here we developed an orgnew package named orgmanager. This class encapsulates methods related to database operations. The correctness of the program that can be debugged through main.

The code for adding a new organization and obtaining tree structure information through XML is provided here. The tree structure is implemented through recursion.

Package orgnew; // the package of the Java class
Import tool .*;
Import java. SQL .*;
Import java. util. *; // reference
Public class orgmanager {
Public orgmanager (){}
Public static void main (string [] ARGs) throws exception {
Connection conn = tool. conntool. getdirectconnection (); // reference the data catalog class
Conn. setautocommit (false );

orgmanager orgmanager1 = new orgmanager ();
orgmanager1.createorg (0, Conn ); // test whether the organizational unit is correctly established
}< br> // create an organizational unit
Public static int createorg (INT parentorgid, connection conn) throws
exception {
string SQL = "insert into Org (orgname, parentorgid) values ('new org ',?) ";
preparedstatement pstat = Conn. preparestatement (SQL);
pstat. setint (1, parentorgid);
pstat.exe cuteupdate ();
pstat. close ();

Statement stat = conn. createstatement ();
String sql2 = "select max (orgid) from org ";
Resultset rs = stat.exe cutequery (sql2 );
Rs. Next ();
Int orgid = Rs. getint (1 );
Rs. Close ();
Stat. Close ();
System. Out. println (orgid );
Return orgid;
}
}
// Recursively obtain the XML format data of organization information
Public static string gettree (connection conn) throws
Exception {
Stringbuffer ret = new stringbuffer (); // you can specify a writable stream that can be buffered.
Ret. append ("<? XML version = '1. 0' encoding = 'gb2312 '?> <Tree id = '0'> "); // defines header information in XML format.
Ret. append ("<item child = '1' text = 'org 'Id = '1'>"); // Insert the node body. Note Tree nodes are marked with items
Ret. append (getchildtree (1, Conn ));
Ret. append ("</item>"); // end mark of the node body
Ret. append ("</tree>"); // indicates the end of the tree.
Return ret. tostring (); // return the response stream
}

Public static string getchildtree (INT orgid, connection conn) throws
Exception {
Stringbuffer ret = new stringbuffer ();
String SQL = "select a. orgid, A. orgname, A. orgcode, count (B. parentorgid) from org A" +
"Left join org B on A. orgid = B. parentorgid" +
"Where a. parentorgid =? "+
"Group by A. orgid, A. orgname ";
Preparedstatement pstat = conn. preparestatement (SQL );
Pstat. setint (1, orgid );
Resultset rs = pstat.exe cutequery ();
While (Rs. Next ()){
Int childorgid = Rs. getint (1 );
String childorgname = Rs. getstring (2 );
String childorgcode = Rs. getstring (3 );
If (childorgcode = NULL ){
Childorgcode = "";
}
If (childorgname = NULL ){
Childorgname = "New Organization ";
}
Int childcount = Rs. getint (3 );
If (childcount> 0 ){
Childcount = 1;
}
Ret. append ("<item child = '" + childcount + "'text ='" + childorgname +
"'Id = '" + childorgid + "'Code ='" + childorgcode + "'> ");
Ret. append (getchildtree (childorgid, Conn ));
Ret. append ("</item> ");
}
Rs. Close ();
Pstat. Close ();
Return ret. tostring ();
}

For other code, see the orgmanager. Java file.

VII. Summary

This document describes the details of Ajax development through an instance. By combining with J2EE, the layer-3 distributed development can be divided into backend and front-end calls. Read, access, and display data.

Through this example, we can see that AJAX separates the web interface from the application. The separation of data and presentation is conducive to the division of labor and cooperation, reducing web application errors caused by non-technical staff Modification on the page, improving efficiency, and more suitable for the current release system. You can also transfer the previous server workload to the client, which facilitates the idle processing capability of the client.

Ajax is a transformation of traditional Web applications. Previously, the server generated HTML pages each time and returned them to the client (browser ). The emergence of Ajax concepts opens the prelude to the non-refresh and Update page era. It is a milestone to replace the trend of updating web pages by submitting forms in traditional web development.

In short, Ajax is suitable for Web applications with high interactions, frequent data reading, and good data classification.

7 pages in total. 9 7 1 2 3 4 5 6 7
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.