Java database connection-configure the Struts Data Source

Source: Internet
Author: User

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 ");
}
 

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.