tomcat 数据源配置

来源:互联网 发布:java的stringbuffer 编辑:程序博客网 时间:2024/06/05 09:22

mysql

 

一、下载驱动包放 入   \Tomcat 7.0\lib

 

二、编辑Tomcat 7.0\conf\context.xml  添加:

 

<Resource name="jdbc/shishicaiDS" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="xxxx" password="xxxx" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/shishicai"/>

 

 

三、在应用web.xml中添加

 <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/shishicaiDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

四、应用程序中

 

 try{
        Context initContext = new InitialContext();
        Context envContext  = (Context)initContext.lookup("java:/comp/env");
        DataSource ds = (DataSource)envContext.lookup("jdbc/shishicaiDS");
        Connection conn = ds.getConnection();
        conn.close();
}catch(Exception e){
        System.out.println(e) ;

}

原创粉丝点击