读书人

Tomcat 六 数据源配置

发布时间: 2012-10-25 10:58:58 作者: rapoo

Tomcat 6 数据源配置

I.

?

%Tomcat_Home%\conf\ 下面的context.xml 在<Context>中加入下面的代码

?

<Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="10" maxWait="1000" username="scott" password="tiger" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:ORCL"/>

?

?

II.

?

编辑项目下的web.xml文件,在<web-app>中加入下面的代码

?

<resource-ref>
???? <description>Tomcat Data Source</description>
???? <res-ref-name>jdbc/oracle</res-ref-name>
???? <res-type>javax.sql.DataSource</res-type>
???? <res-auth>Container</res-auth>
?</resource-ref>

?

III.

?

建一个servlet测试一下:

?

 Connection conn = null;   try    {        Context initContext = new InitialContext();        Context envContext = (Context)initContext.lookup("java:/comp/env");        DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");        conn = ds.getConnection();   if(conn != null)   {  out.println("Tomcat数据源连接成功!");  } } catch(Exception e) {   e.printStackTrace(); }

?

读书人网 >Web前端

热点推荐