7. Configure the Struts Data Source
General steps:
7.1 configure the JNDI Data Source
Add a configuration similar to the following under the struts-config.xml configuration file <struts-config> Tab
<Data-source> attribute type is used to define the data source javax. SQL. DataSource implementation class: org. apache. commons. dbcp. BasicDataSource
<Set-property> for details about tag attribute settings, refer to the implementation class org. apache. commons. dbcp. definitions of database connection-related attributes in BasicDataSource. <set-property> is used to set its content based on the setXXX () method in the implementation class. this is like <jsp: setProperty> setting bean properties.
<Data-sources>
<Data-source type = "org. apache. commons. dbcp. BasicDataSource">
<Set-property = "driverClassName" value = "com. mysql. jdbc. Driver"/>
<Set-property = "url" value = "jdbc: mysql: // localhost: 3306/mydb"/>
<Set-property = "username" value = "root"/>
<Set-property = "password" value = "root"/>
<Set-property = "maxActive" value = "30"/>
<Set-property = "maxIdle" value = "10"/>
<Set-property = "maxWait" value = "1000"/>
</Data-source>
</Data-sources>
7.2 obtain a data source instance
The following two methods are defined in the org. apache. struts. action. Action class:
Protected DataSource getDataSource (HttpServletRequest request, String key)
Protected DataSource getDataSource (HttpServletRequest request)
Key code:
DataSource ds = this. getDataSource (request );
View the source code of the org. apache. struts. action. Action class:
/**
* <P> Return the specified data source for the current module. </p>
*
* @ Param request The servlet request we are processing
* @ Param key The key specified in the <code> & lt; data-sources & gt; </code>
* Element.
*
* @ Since Struts 1.1
*/
Protected DataSource getDataSource (HttpServletRequest request, String key ){
// Identify the current module
ServletContext context = getServlet (). getServletContext ();
ModuleConfig moduleConfig =
ModuleUtils. getInstance (). getModuleConfig (request, context );
Return (DataSource) context. getAttribute (key + moduleConfig. getPrefix ());
}
Protected DataSource getDataSource (HttpServletRequest request ){
Return (getDataSource (request, Globals. DATA_SOURCE_KEY ));
}
We can see that the data source needs to be obtained from the custom Action processing class (inherited from the Action.
Parameter description:
Request a user request
Key <data-source> the key attribute set by the tag. If the key attribute is not set, the default value is Globals. DATA_SOURCE_KEY.
Use the protected DataSource getDataSource (HttpServletRequest request) method to obtain the data source
The data source whose key value is Globals. DATA_SOURCE_KEY is called by default.
Reference from struts-config_1_2.dtd
Key Servlet context attribute key under which this data source
Will be stored. Default is the value specified by string
Constant defined by Globals. DATA_SOURCE_KEY. The application
Module prefix (if any) is appended to the key
($ {Key} $ prefix }).
[Org. apache. struts. Globals. DATA_SOURCE_KEY]
Note: The permission for this method is protected, because it can be used in the subclass of different packages and cannot be called in different non-subclass packages. in addition, the custom Action processing class constructor is called only during the first request. therefore, the single-instance mode may be used to instantiate a custom Action processing class.
7.3 database operations
General steps:
A) Establish a database connection through the data source instance
B) Create a Statement object
C) obtain the result set
D) Shut Down database Connection resources (including ResultSet, Statement, and Connection instances)
Key code:
Connection conn = ds. getConnection ();
PreparedStatement stmt = conn. prepareStatement (SQL );
Stmt. setString (1, username );
Stmt. setString (2, password );
ResultSet rs = stmt.exe cuteQuery ();
If (rs. next ())
{
Request. setAttribute ("hint", bundle. getString ("login. check. hint. success "));
Return mapping. findForward ("success ");
}