tomcat6連接池

1.配置文件  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.測試類
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"));
        }
    }
}
發佈了60 篇原創文章 · 獲贊 7 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章