1. configuration file WebRoot/META-INF/context. xml
<Context reloadable = "true">
<WatchedResource> WEB-INF/web. xml </WatchedResource>
<Resource name = "jdbc/mysql" auth = "Container" type = "javax. SQL. DataSource"
MaxActive = "100"
MaxIdle = "30"
MaxWait = "10000"
Username = "root"
Password = "root"
DriverClassName = "com. mysql. jdbc. Driver"
Url = "jdbc: mysql: // 118.186.210.242: 447/wucaiju? User = root & amp; password = root & amp; useUnicode = true & amp; characterEncoding = UTF-8 "/>
</Context>
2. WebRoot/WEB-INF/web. xml
<Resource-ref>
<Res-ref-name> jdbc/mysql </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
</Resource-ref>
3. Test class
Public class TestDao {
Public void test () throws Exception {
Context context = new InitialContext ();
DataSource ds = (DataSource) context. lookup ("java:/comp/env/jdbc/mysql ");
JdbcTemplate jdbcTemplate = new JdbcTemplate (ds );
JdbcTemplate. afterPropertiesSet ();
System. out. println (jdbcTemplate );
String SQL = "select * from t_pic ";
List list = jdbcTemplate. queryForList (SQL );
For (int I = 0; I <list. size (); I ++ ){
Map map = (Map) list. get (I );
System. out. println (map. get ("createTime "));
}
}
}