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 ·····