Open white: See you still remember that I don't remember that I am in the "fifth article" "MYDOG" can be "MyDog"? Now you can't, I will tell you how you can tell you. "
Program requirements: The program starts, the first page is displayed, with such information, a drop-down box is used to select "The user's department name", this drop-down box is from the "department information table" (when the page It is also available when it is generated. Then press the "Submit" button to display all the staff of the department.
Analysis: Because the page will get data from the "Department", I have to use a way to generate the Action to get data. The display of data is no longer single data, so it is designed to a loop on the page. A data displayed is no longer a data.
Create a data table: Remember the database we established in the previous one.
The database name is "User" already has a table called "useertable", and we now build one in this database.
Table "DepatTable"
Department number depatno text
Department name depatname text
Fill in a few values:
001 Production Department
002 Engineering Department
003 Personnel Department
Program start
From now on, we will do according to the idea of true project development, the division of the module will be meticulously
A Module: Provide database connections
B Module: ActionandForm Module
C Module: Business Logic, Providing Data Operation
D Module: Data Entity Package
These modules are actually the concept of "package", and I don't want to say "package" in Eclipse.
A module (2 classes) - Package DataConnectFactory
ConnectFactory class
// Data Connection Factory
Package DataconnectFactory;
Import java.sql. *;
Public Class ConnectFactory
{
/ / Define the driver of the database (hereinafter given below is to connect the ODBC database)
Private static string strodr = "sun.jdbc.odbc.jdbcodbcdriver";
/ / Define the URL of the database (name)
Private static string strconnection = "JDBC: ODBC: User";
// Username
Private static string strusername = "";
// Database Access password
Private static string strpassword = "";
Public Static Connection getConnectionBydriver ()
Throws myexception
{
Connection conn = NULL;
Try
{
Class.Forname (strDriver); // Register driver
CONN = // Get a connection
Drivermanager.getConnection (STRCONNECTION,
Strusername,
Strpassword;
}
Catch (ClassNotFoundException EX1)
{
EX1.PRINTSTACKTRACE ();
Throw New MyException ("Class Not Found!");
}
Catch (SQLEXCEPTION EX2)
{
EX2.PRINTSTACKTRACE ();
Throw new myexception ("sql error");
Finally {}
Return conn;
}
}
MyException class
Package DataconnectFactory;
// Define your own anomaly
Public class myexception extends Exception
{
Public myException ()
{
Super ();
}
Public myException (String Message)
{
SUPER (Message);
}
}
B module (3 classes) - Package Action
UserAction class
Package action;
// Get the information of the department and give the page when the first query page is generated.
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.Actionerror;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.actionForward;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.Actions.dispatchaction;
Import Biz.Searchbiz;
Import Entity. *;
Public Class UseerAction Extends Dispatchction
{
// Go to the query interface
Public ActionForward Tosearch (ActionMapping Mapping,
Actionform Form,
HTTPSERVLETREQUEST REQUEST,
Httpservletresponse response
Throws Exception
{
/ / Define Error Information Package
ActionerroS Errors = NULL;
// Get page form information
Userform uf = (userform) Form;
// Define an array of departmental entities
Departmentity [] departs = null;
Searchbiz searchbizdept = null;
Try
{
Searchbizdept = new searchbiz ();
DEPARTS =
Searchbizdept.selectDeptentitys
New departmentent ());
Uf.setDeparts (departs);
}
Catch (Exception E)
{
ERRORS = New ActionerRORS ();
E.PrintStackTrace ();
Errors.Add (ActionerRors.global_ERROR, New Actionerror ("System.exception");
Return mapping.findforward ("error");
}
Finally
{
IF (ErrorS! = NULL)
{
SaveerRors (Request, Errors);
}
}
Return mapping.findforward ("tosearch");
}
}
Package action;
Userform class
// Pack all solid into FORM
Import org.apache.struts.action.Actionform; import entity. *;
Public Class Userform Extends ActionForm
{
Private uSERENTITY USER = new useERENTITY ();
Private uSERENTITY [] Users = NULL;
PRIVATE DEPARTENTENTITY Department ();
Private departmentity [] departs = null;
Public departmentity getDepart ()
{
Return depart;
}
Public departmententity [] getDeparts ()
{
Return departs;
}
Public useERENTITY GETUSER () {
Return User;
}
Public useerentity [] getUsers ()
{
Return User;
}
Public void setDepart (DepartMentent Repart)
{
THIS.DEPART = DEPART;
}
Public void setDeparts (departmentity [] departs)
{
THIS.DEPARTS = departs;
}
Public void setuser (UserErentity User)
{
THIS.USER = User;
}
Public void setUsers (useers)
{
THIS.USERS = User;
}
}
ResultAction
Package action;
/ / When the page is submitted, check according to the conditions submitted by the page.
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.actionForward;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.Actions.dispatchaction;
Import Biz.Searchbiz;
Import Entity. *;
Public Class ResultAction Extends Dispatchction
{
Public ActionForward Execute (ActionMapping Mapping,
Actionform Form,
HTTPSERVLETREQUEST REQUEST,
Httpservletresponse response
Throws Exception
{
/ / Define Error Information Package
ActionerroS Errors = NULL;
// Get page form information
Userform uf = (userform) Form;
/ / Define the user entity array
Userentity [] useERENTITYS = NULL;
/ / Define user entities
UseERENTITY = New useERENTITY ();
// Get the data submitted on the page
Userentity = uf.getuser ();
System.out.println (Userentization.getDepartNo ()); Searchbiz Searchbiz = NULL;
Try
{
Searchbiz = new searchbiz ();
Userentitys = Searchbiz.SelectUserentitys (UseERENTITY);
Uf.setUsers (useERENTITYS);
}
Catch (Exception E)
{
Return mapping.findforward ("error");
}
Finally
{
}
Return mapping.findforward ("Success");
}
}
C Module (1 class) - Package Biz
Searchbiz class
Package Biz;
// Here, complete all data of all data such as query and data conversion operations.
Import java.sql.connection;
Import java.sql.preparedStatement;
Import java.sql.resultset;
Import java.sql.sqlexception;
Import java.util.arraylist;
Import DataconnectFactory.ConnectFactory;
Import DataconnectFactory.myException;
Import Entity. *;
Public Class Searchbiz
{
PRIVATE STRING STRDRIVER;
PRIVATE STRING STRCONNECTION;
Private string strusename;
PRIVATE STRING STRPASSWORD;
// Query user information
Public useERENTITY [] SELECTUSERENTITITYS (UserErentity Userentity)
Throws myexception
{
Userentity [] Employeeentitys = NULL;
Connection conn = NULL;
PreparedStatement PSTMT = NULL;
ResultSet RS = NULL;
StringBuffer SBSQL = New StringBuffer ();
ArrayList List = NULL;
Try
{
CONN = ConnectFactory.getConnectionBydriver ();
/ / Generate SQL
SBSQL.Append ("SELECT");
SBSQL.Append ("UserTable.Userno,");
SBSQL.Append ("UserTable.DepartNo,");
SBSQL.Append ("DEPArtTable.DepartName);
SBSQL.Append ("from userTable, departtable);
SBSQL.Append ("WHERE
UserTable.Departno = '" uSrentity.getDEPartNO () "' ");
SBSQL.Append ("and");
SBSQL.Append ("useertable.Departno = departtable.Departno);
System.out.println (sbsql.tostring ());
PSTMT = conn.preparestatement (sbsql.tostring ()); rs = pstmt.executeQuery ();
List = Parsearray (RS, 1);
List.trimtosize ();
Employeentitys = new useERENTITY [list.size ()];
IF (Employeeentitys! = null && list.size ()> 0)
{
EMPLOYEETITYS =
(UseERENTITY []) List.toArray (Employeeentitys);
}
}
Catch (MyException EX1)
{
EX1.PRINTSTACKTRACE ();
Throw EX1;
}
Catch (SQLEXCEPTION EX2)
{
EX2.PRINTSTACKTRACE ();
Throw new myexception ("sql error");
}
Finally // Database Access is completed, close the relevant connection,
// Release the resources, you should do it
{
Try
{
IF (rs! = null)
{
Rs.close ();
RS = NULL;
}
IF (PSTMT! = NULL)
{
PSTMT.Close ();
PSTMT = NULL;
}
}
Catch (SQLException E)
{
E.PrintStackTrace ();
}
}
Return Employeeentitys;
}
// Query department information
Public Departmentity [] SelectDeptentitys (DepartMentent Department "
Throws myexception
{
Departmententity [] departmententitys = NULL;
Connection conn = NULL;
// previously used Statement, now use PrepareStatement
// This is because PrepareStatement has a precompiled process
PreparedStatement PSTMT = NULL;
ResultSet RS = NULL;
StringBuffer SBSQL = New StringBuffer ();
ArrayList List = NULL;
Try
{
CONN = ConnectFactory.getConnectionBydriver ();
SBSQL.Append ("SELECT Departmentno, DepartName from Dep AttersTable");
PSTMT = conn.preparestatement (sbsql.tostring ()); // Precompiled
RS = pstmt.executeQuery (); //
List = Parsearray (RS, 2); // Convert the result set to entity arrays
List.trimtosize ();
DEPARTMENTENTITYS = new departmentity [list.size ()];
IF (departmententitys! = null && list.size ()> 0)
{
Departmententitys =
(DEPARTMENTENTINTINTINTIN (DEPARTMENTENTITYS);
}
}
Catch (MyException EX1)
{
EX1.PRINTSTACKTRACE ();
Throw EX1;
}
Catch (sqlexception ex2) {
EX2.PRINTSTACKTRACE ();
Throw new myexception ("sql error");
}
Catch (Exception EX3)
{
EX3.PRINTSTACKTRACE ();
Throw new myexception ("exception");
}
Finally // Database Access is completed, close the relevant connection,
// Release the resources, you should do it
{
Try
{
IF (rs! = null)
{
Rs.close ();
RS = NULL;
}
IF (PSTMT! = NULL)
{
PSTMT.Close ();
PSTMT = NULL;
}
}
Catch (SQLException E)
{
E.PrintStackTrace ();
}
}
Return departmententitys;
}
/ / Convert the result set to the method of entity arrays
Protected Arraylist Parsearray (ResultSet RS, Long LTYPE) THROWS SQLEXCEPTION
{
ArrayList List = New ArrayList ();
UserErentity Userentity = NULL;
DEPARTMENTENTENTITY Department;
Object Object = NULL;
Try {
While (rs.next ())
{
IF (LTYPE == 1)
{
Userentity = new useERENTITY ();
Userentity.setUserno ("Userno");
Userentity.SetDepartNO (RS.GetString ("departno");
Userentity.setDepartName (RS.GetString ("departname"));
Object = (Object) uSrentity;
}
Else
{
DEPARTMENTENTITY = New departmentent ();
Departmententity.SetDepartmentNo (Rs.getstring ("departno");
Departmententity.SetDePartmentName (Rs.getstring ("DepartName"));
Object = (Object) DEPARTMENTENTENTITY;
}
List.add (Object);
}
}
Catch (SQLException EX)
{
EX.PrintStackTrace ();
Throw EX;
}
Finally {}
Return List;
}
}
D Module (2 classes) - package entity
Userentity class
Package Entity;
// user information entity
Public Class useERENTITY
{
PRIVATE STRING Userno = NULL;
Private string userpassword = null;
PRIVATE STRING DEPARTNO = NULL;
PRIVATE STRING Departmentname = NULL;
Public String getDepartName ()
{
RETURN DepartName == NULL? ": DepartName.trim ();
}
Public string getDepartno () {
RETURN Departno == NULL? ": Departno.trim ();
}
Public string getuserno ()
{
RETURN Userno == NULL? ": Userno.trim ();
}
Public String getUserpassword ()
{
RETURN Userpassword == NULL? "": Userpassword.trim ();
}
Public void setDepartName (String Departname)
{
this.DepartName = departname;
}
Public void setDepartNO (String Departno)
{
THIS.DEPARTNO = DEPARTNO;
}
Public void setUserno (String Userno)
{
THIS.USERNO = Userno;
}
Public void setuserpassword (String Userpassword)
{
THIS.USERPASSWORD = Userpassword;
}
}
Departmententity class
Package Entity;
// Department information entity
Public class departmententity
{
PRIVATE STRING DepartmentNO = NULL;
Private string departmentname = null;
Public String getDepartmentName ()
{
RETURN DepArtmentName == NULL? "": DepartmentName.trim ();
}
Public String getDepartmentNo ()
{
RETURN DepArtNO == NULL? ": DepartmentNo.trim ();
}
Public void setDepartmentName (String DepartmentName)
{
THIS.DEPARTMENTNAME = DepartmentName;
}
Public void setDepartmentno (String Department "
{
THIS.DEPARTNO = DEPARTNO;
}
}
Profile: struts-config.xml
Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
This Is The Struts Configuration File for the Example Application,
Using the proposed New Syntax.
NOTE: You Would Only Flesh Out The Details in The "Form-Bean"
Declarations if you had a generator Tool That Used Them To create
THE CORRESPONDING JAVA CLASSES for you. Otherwise, you would notlement only the "form-bean" Element itself, with the corresponding
"Name" and "type" attributes.
->
form-bean>
form-beans>
global-forwards>
TYPE = "action.useraction" Name = "Userform" Scope = "request" Parameter = "Operate" VALIDATE = "false" INPUT = "Search.jsp"> action> Type = "action.resultation" name = "userform" Scope = "request" INPUT = "Search.jsp"> action> action-mappings> Struts-Config> JSP page Search.jsp <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%> <% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%> <% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%> <% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>