How to call the data-source configured in the struts-config.xml

Source: Internet
Author: User
Notes for configuring data-source in struts-config.xml: lt; data-sourcesgt; lt ;! -- Name: data-source description: The data-source element defines a specific

Notes for configuring data-source in struts-config.xml: lt; data-sourcesgt; lt ;! -- Name: data-source description: The data-source element defines a specific

Notes for configuring data-source in the struts-config.xml:





Value = "Oracle. jdbc. driver. OracleDriver"/>

Value = "jdbc: oracle: thin: @ localhost: 1521: orcl"/>








Code in the ** DAO file:
First define
Private DataSource;
Constructor of this class:
Public ** DAO (DataSource dataSource)
{
This. dataSource = dataSource;
}
Then, use dataSource. getConnection () to obtain the connection in the method of this class.
Be sure not to use static methods


There are two methods to call the configured datasource in the action class:
Method 1: ServletContext context = servlet. getServletContext (); // obtain the attributes of the configuration file in Struts
DataSource dataSource = (DataSource) context. getAttribute ("dataSource ");

Method 2: DataSource ds = this. getDataSource (request, "dataSource ");
** DAO ** dao = new ** DAO (ds );

The getDataSource (HttpServletRequest request, String key) method is to call the configured data-source in the action.

Of course, the parameter key in the method is the one you configured Key

The comparison method 2 is concise! It took a lot of effort to get it out ·····

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.