RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>JSP>列表

配置tomcat连接池

来源: 作者: 时间:2007-08-25 点击:
今天,配了半下午的连接池,最后测试终于成功了,当时可高兴了!呵呵!现在贴出了,oracle和Mysql的配置方法!希望对大家有所帮助!
 /**oracle配置方法*/
一,修改server.xml
在server.xml的</Host>前加入
----------------------------------------
<Context path="/test" docBase="test" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myoracle">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@127.0.0.1:1521:smsdb</value>
</parameter>
<parameter>
<name>username</name>
<value>user1</value>
</parameter>
<parameter>
<name>password</name>
<value>pass1</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>300</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
</ResourceParams>
</Context>
----------------------------------------
 
二,把oracle的jdbc/lib目录下的classes12.jar拷贝到tomcat的common/lib目录下
 
三,测试程序test.jsp
----------------------------------------
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%>
 
<%
DataSource ds = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/myoracle");
if(ds!=null){
out.println("Connection is OK!");
Connection cn=ds.getConnection();
if(cn!=null){
out.println("cn is Ok!");
Statement stmt = cn.createStatement();
ResultSet rst = stmt.executeQuery("select * from test");
out.println("<p>rst is Ok!" + rst.next());
while(rst.next()){
out.println("<P>BOOK_CODE:" + rst.getString(1));
}
cn.close();
}else{
out.println("rst Fail!");
}
}
else
out.println("Fail!");
}catch(Exception ne){ out.println(ne);
}
%>
----------------------------------------
----------------------------------------
-----MySql连接池配置方法--------
一、在C:\Tomcat 5.5\conf\server.xml的
    <Host>和</Host>添加如下数据源配置
 <Host>
   <Context path="/cc" docBase="cc" debug="0" reloadable="true" crossContext="true">
            <Logger className="org.apache.catalina.logger.FileLogger"
              prefix="localhost_DBTest_log." suffix=".txt"
               timestamp="true"/>
 <Resource
    name="jdbc/mysql"
    type="javax.sql.DataSource"
    password="caimingyu"
    driverClassName="com.mysql.jdbc.Driver"
    maxIdle="2"
    maxWait="5000"
    username="root"
    url="jdbc:mysql://localhost:3306/test"
    maxActive="4"/>
   </Context>
  </Host>
 
二、把mySQL的mysql-connector-java-5.0.4-bin.jar包,加到c:\Tomcat 5.5\common\lib目录下.
三、编写程序测试test.jsp
 
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%>
<%
DataSource ds = null;
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/mysql");
if(ds!=null){
out.println("Connection is OK!");
Connection cn=ds.getConnection();
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from ta");
while(rs.next()){
  out.println(rs.getString("name"));
}
}
%>
这样就搞定了
如果出错的话,在本工程的web.xml的加入
   <webapp> 
    <resource-ref>
     <description>DB Connection</description>
     <res-ref-name>jdbc/mysql</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
   </resource-ref></webapp>
 

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
Google Adsense
相关文章