配置数据源

 
1.META-INF/context.xml  或 Tomcat里的conf-context.xml

<?xml version='1.0' encoding='utf-8'?> 
<Context> 
 <Resource name="jdbc/Name"
            auth="Container"
            type="javax.sql.DataSource"
            username="root"
            password="123qwe"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/struts?CharacterEncoding=GBK"
            maxActive="8"
            maxIdle="4"/>
</Context>

 

解说:
name 表示指定的JNDI名称
auth 表示认证方式,一般为Container
type 表示数据源床型,使用标准的javax.sql.DataSource
maxActive 表示连接池当中最大的数据库连接
maxIdle 表示最大的空闲连接数
maxWait 当池的数据库连接已经被占用的时侯,最大等待时间
logAbandoned 表示被丢弃的数据库连接是否做记录,以便跟踪
username 表示数据库用户名
password 表示数据库用户的密码
driverClassName 表示JDBC DRIVER
 
2.JDBC驱动,放在%TOMCAT_HOME%/lib
3.配置项目的web.xml文件。
将下面代码加入到web.xml中:

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

 

6.将项目拷贝到TOMCAT_HOME/webapps下面。

7.启动tomcat,用代码:

     Context ctx = new InitialContext();
     DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Name");
     Connection conn = ds.getConnection();//可以使用conn操作了。
  
  ....

 
    测试是否配置成功。
 
=======================================================
方法2
Tomcat里的conf-context.xml:

 

<Context path="/zhinangtuan" docBase="F:\Eclipse\MyZNTProject\MyZhiNangTuanDemo\WebRoot"
  debug="5" reloadable="true" crossContext="true">
 
<Logger className="org.apache.catalina.logger.FileLogger"
     prefix="localhost_MysqlTest_log." suffix=".txt"
     timestamp="true"/>
     
<Resource name="jdbc/mldn"  auth="Container" type="javax.sql.DataSource"/>
 
 <ResourceParams name="jdbc/mldn">
  <parameter>
   <name>factory</name>
   <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
  </parameter>
  <parameter>
   <name>maxActive</name>
   <value>100</value>
  </parameter>
  <parameter>
   <name>maxIdle</name>
   <value>30</value>
  </parameter>
  <parameter>
   <name>maxWait</name>
   <value>5000</value>
  </parameter>
  <parameter>
   <name>username</name>
   <value>root</value>
  </parameter>
  <parameter>
   <name>password</name>
   <value>mysqladmin</value>
  </parameter>
  <parameter>
   <name>driverClassName</name>
   <value>org.gjt.mm.mysql.Driver</value>
  </parameter>
  <parameter>
   <name>url</name>
   <value>jdbc:mysql://localhost:3306/mldn</value>
  </parameter>
   </ResourceParams>
</Context>

 解说
<Host name="www.yxq.com"  appBase="D:/JSPWeb" unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
   <Context path="/city" docBase="01_CityInfo" debug='0' reaload="true"/>
</Host>

<Host>元素用来创建主机,
 name属性指定了主机名(域名),
 appBase属性指定了Java Web应用程序存放在本地计算机中的位置。
<Context>元素用来配置主机的Web应用程序,
 path属性指定了访问主机中某个Web应用的路径,
 docBase属性指定了相对于D:/JSPWeb目录下的Java Web应用程序路径。
所以,若访问www.yxq.com/city路径,既可访问D:/JSPWeb目录下的01_CityInfoWeb应用程序,也可以将path属性设置为“/”,
这样直接访问www.yxq.com即可访问01_CityInfoWeb应用程序。
----
contex指上下文,实际上就是一个web项目;
path是虚拟目录,访问的时候用127.0.0.1:8080/welcom/*.jsp访问网页,welcome前面要加/;
docBase是网页实际存放位置的根目录,映射为path虚拟目录;
reloadable="true"表示你修改了jsp文件后不需要重启就可以实现显示的同步。

eg:

server.xml中添加
<Context path="/helloapp" docBase="E:\tomcat 5.5.20\webapps\helloapp\WebRoot" reloadable="true"/>
然后在浏览器中敲入
http://localhost:8028/helloapp/hello.jsp
就可以显示E:\tomcat 5.5.20\webapps\helloapp\WebRoot\hello.jsp
中的hello.jsp了

====================
测试:

bean:----
        Context initCtx = new InitialContext();  
        Context envCtx = (Context) initCtx.lookup("java:comp/env");  
        DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");  
        conn = ds.getConnection();  

JSP:----
  <%@page import="java.sql.*"%>
  <%@ page import="zhinangtuan.test.conn_test" %>
  <% conn_test connt = new conn_test();
   Connection conn = connt.conn();
   if(conn!=null) out.print("存在");
   else out.print("不存在");
   
   PreparedStatement pst = conn.prepareStatement("select * from admin");
   ResultSet rs = pst.executeQuery();
   while(rs.next()){
   out.print(rs.getString(1)+ " " + rs.getString(2)+"  ");
   }
   %>
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值