tomcat 8 通过jndi配置数据源


1、测试页面/usr/locat/tomcat/webapp/dbtest/test.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
< sql:query  var = "rs"  dataSource = "jdbc/TestDB" >
     select id, foo, bar from testdata
</ sql:query >
< html >
< head >
     < title >DB Test</ title >
</ head >
< body >
     < h2 >Results</ h2 >
     < c:forEach  var = "row"  items = "${rs.rows}" >
         Foo ${row.foo}< br />
         Bar ${row.bar}< br />
     </ c:forEach >
</ body >
</ html >


2.应用配置文件/usr/local/tomcat/webapps/dbtest/WEB-INF/web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
< web-app  xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version = "2.4" >
< description >MySQL Test App</ description >
< resource-ref >
     < description >DB Connection</ description >
     < res-ref-name >jdbc/TestDB</ res-ref-name >
     < res-type >javax.sql.DataSource</ res-type >
     < res-auth >Container</ res-auth >
</ resource-ref >
</ web-app >


3.上下文文件/usr/local/tomcat/conf/context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<? xml  version = '1.0'  encoding = 'utf-8' ?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
< Context >
     <!-- Default set of monitored resources -->
     < WatchedResource >WEB-INF/web.xml</ WatchedResource >
     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
     <!--
     <Manager pathname="" />
     -->
     <!-- Uncomment this to enable Comet connection tacking (provides events
          on session expiration as well as webapp lifecycle) -->
     <!--
     <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
     -->
     < Resource  name = "jdbc/TestDB"  auth = "Container"  type = "javax.sql.DataSource"
     maxTotal = "100"  maxIdle = "30"  maxWaitMillis = "10000"
     username = "javauser"  password = "javadude"
     driverClassName = "com.mysql.jdbc.Driver"
     url = "jdbc:mysql://10.13.106.11:3306/javatest" />
</ Context >


4、建立数据库

1
2
3
4
5
6
mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@'10.13.106.%' IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql> create database javatest;
mysql> use javatest;
mysql> create table testdata (id int not null auto_increment primary key,foo varchar(25),bar int);
mysql> insert into testdata values(null, 'ok', 12345);
mysql> flush privileges;


5、上传taglibs-standard-impl-1.2.5.jar、taglibs-standard-spec-1.2.5.jar和mysql-connector-java-5.1.10-bin.jar到/usr/local/tomcat/lib

   

6、重启tomcat




     本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1793086,如需转载请自行联系原作者




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值