TOMCAT5.5.23配置全局数据源

环境:
XP系统,JDK1.5.09,TOMCAT5.5.23和MYSQL5.0.37安装版.

设置:
1.打开MYSQL,创建库:create database testDB,使用这个库,在里面创建一张表:create table person(id Integer primary key auto_increment, name varchar(32), age int(3));插入1条纪录:insert into person values(100, 'chen', 23).
2.打开TOMCAT安装目录下的CONF目录,找到SERVER.XML文件,打开它.在SERVER.XML中写到:
<GlobalNamingResources>
.......................
<Resource
  type="javax.sql.DataScource"
  name="jdbc/mysql"
  driverClassName="com.mysql.jdbc.Driver"
  maxActive="10"
  maxIdle="3"
  maxWait="10000"
  username="root"
  password="你自己的数据库密码"
  url="jdbc:mysql://localhost:3306/testdb"/>
</GlobalNamingResources>

在</HOST>之前写入:
<Context path="/testdb" privileged="true" docBase="testdb">
</Context>
3.在CONTEXT.XML中写入:
<Context>
.........
  <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource"/>
4.在应用testdb的WEB-INF目录下的WEB.XML中写入:
<web-app>
  <resource-ref>
    <description>mysql connection</description>
    <res-ref-name>jdbc/mysql</res-ref-name>
    <res-type>javax.sql.DataScource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>
5.在应用中写一个test.jsp测试程序:
<%@page import="java.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
<%@page contentType="text/html;charset=gb2312"%>
<html>
  <head><title>test</title></head>
  <body>
    <%
      java.sql.Connection con = null;
      Statement stem = null;
      ResultSet rs = null;
      try{
        Context ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
        con = ds.getConnection();
        stem = con.createStatement();
        rs = stem.executeQuery("select * from person");
        while(rs.next()){
          out.print(rs.getInt(1) + ":" + rs.getString(2) + ":" + rs.getInt(3) + ".");
        }
      }catch(SQLException e){
      }finally{
        rs.close();
        stem.close();
        con.close();
      }
    %>
  </body>
</html>
6.测试,运行TOMCAT,打开网页输入http://localhost:8080/testdb/test.jsp,如果能看到数据库中刚保存的数据,那就表示配置成功.
注:如果想给其他的应用也配置这个数据源,那么从第4步开始,换个应用设置就可以了.

 
============================================<br><br>Out of the box, Tomcat 5.5 requires the Java 2 Standard Edition Runtime<br>Environment (JRE) version 5.0 or later. However, you can also run Tomcat<br>5.5 on earlier versions of the JRE, as detailed below.<br><br>=============================<br>Running With JRE 5.0 Or Later<br>=============================<br><br>(1) Download and Install the J2SE Runtime Environment (JRE)<br><br>(1.1) Download the Java 2 Standard Edition Runtime Environment (JRE),<br> release version 5.0 or later, from http://java.sun.com/j2se.<br><br>(1.2) Install the JRE according to the instructions included with the<br> release.<br><br>(1.3) Set an environment variable named JAVA_HOME to the pathname of<br> the directory into which you installed the JRE, e.g. c:\j2sdk5.0<br> or /usr/local/java/j2sdk5.0.<br><br><br>(2) Download and Install the Tomcat Binary Distribution<br><br>NOTE: As an alternative to downloading a binary distribution, you can create<br>your own from the Tomcat source repository, as described in "BUILDING.txt".<br>If you do this, the value to use for "${catalina.home}" will be the "dist"<br>subdirectory of your source distribution.<br><br>(2.1) Download a binary distribution of Tomcat from:<br><br> http://tomcat.apache.org<br><br>(2.2) Unpack the binary distribution into a convenient location so that the<br> distribution resides in its own directory (conventionally named<br> "apache-tomcat-[version]"). For the purposes of the remainder of this document,<br> the symbolic name "$CATALINA_HOME" is used to refer to the full<br> pathname of the release directory.<br><br><br>(3) Start Up Tomcat<br><br>(3.1) Tomcat can be started by executing the following commands:<br><br> $CATALINA_HOME\bin\startup.bat (Windows)<br><br> $CATALINA_HOME/bin/startup.sh (Unix)<br><br>(3.2) After startup, the default web applications included with Tomcat will be<br> available by visiting:<br><br> http://localhost:8080/<br><br>(3.3) Further information about configuring and running Tomcat can be found in<br> the documentation included here, as well as on the Tomcat web site:<br><br> http://tomcat.apache.org<br><br><br>(4) Shut Down Tomcat<br><br>(4.1) Tomcat can be shut down by executing the following command:<br><br> $CATALINA_HOME\bin\shutdown (Windows)<br><br> $CATALINA_HOME/bin/shutdown.sh (Unix)<br><br><br><br>====================================<br>Running Tomcat With J2SE Version 1.4<br>====================================<br><br>(1) Obtain the compat package:<br><br>(1.1) Download the compat package from the binary download site:<br> http://tomcat.apache.org<br><br> * Or build this package yourself from the source code: see <br> "BUILDING.txt" in this directory.<br><br>(2) Unzip the package in $CATALINA_HOME. It will place the XML<br> parser APIs and Xerces implementation in the common/endorsed<br> directory, and the JMX API jar (jmx.jar from Sun) in the bin<br> directory.<br><br>(3) Follow the same directions for starting and stopping the<br> server as if you were using J2SE 5.0.<br><br><br>==================================================<br>Advanced Configuration - Multiple Tomcat Instances<br>==================================================<br><br>In many circumstances, it is desirable to have a single copy of a Tomcat<br>binary distribution shared among multiple users on the same server. To make<br>this possible, you can pass a "-Dcatalina.base=$CATALINA_BASE" argument when<br>executing the startup command (see (2)). In this<br>"-Dcatalina.base=$CATALINA_BASE" argument, replace $CATALINA_BASE with the<br>directory that contains the files for your 'personal' Tomcat instance.<br><br>When you use this "-Dcatalina.base=$CATALINA_BASE" argument, Tomcat will<br>calculate all relative references for files in the following directories based<br>on the value of $CATALINA_BASE instead of $CATALINA_HOME:<br><br>* conf - Server configuration files (including server.xml)<br><br>* logs - Log and output files<br><br>* shared - For classes and resources that must be shared across all web<br> applications<br><br>* webapps - Automatically loaded web applications<br><br>* work - Temporary working directories for web applications<br><br>* temp - Directory used by the JVM for temporary files (java.io.tmpdir)<br><br>If you do not pass the "-Dcatalina.base=$CATALINA_BASE" argument to the<br>startup command, $CATALINA_BASE will default to the same value as $CATALINA_HOME,<br> which means that the same directory is used for all relative path resolutions.<br><br>The administration and manager web applications, which are defined in the<br>$CATALINA_BASE/conf/Catalina/localhost/admin.xml<br>and <br>$CATALINA_BASE/conf/Catalina/localhost/manager.xml files, will<br>not run in that configuration, unless either:<br>- The path specified in the docBase attribute of the Context element is made<br> absolute, and replaced respectively by $CATALINA_HOME/server/webapps/admin<br> and $CATALINA_HOME/server/webapps/manager<br>- Both web applications are copied or moved to $CATALINA_BASE, <br> and the path specified in the docBase attribute of the Context<br> element is modified appropriately.<br>- Both web applications are disabled by removing<br> $CATALINA_BASE/conf/Catalina/localhost/admin.xml<br> and<br> $CATALINA_BASE/conf/Catalina/localhost/manager.xml.<br><br><br>================<br>Troubleshooting<br>================<br><br>There are only really 3 things likely to go wrong during the stand-alone<br>Tomcat install:<br><br>(1) The most common hiccup is when another web server (or any process for that<br> matter) has laid claim to port 8080. This is the default HTTP port that<br> Tomcat attempts to bind to at startup. To change this, open the file:<br><br> $CATALINA_HOME/conf/server.xml<br><br> and search for '8080'. Change it to a port that isn't in use, and is<br> greater than 1024, as ports less than or equal to 1024 require superuser<br> access to bind under UNIX.<br><br> Restart Tomcat and you're in business. Be sure that you replace the "8080"<br> in the URL you're using to access Tomcat. For example, if you change the<br> port to 1977, you would request the URL http://localhost:1977/ in your browser.<br><br>(2) An "out of environment space" error when running the batch files in<br> Windows 95, 98, or ME operating systems.<br><br> Right-click on the STARTUP.BAT and SHUTDOWN.BAT files. Click on<br> "Properties", then on the "Memory" tab. For the "Initial environment" field,<br> enter in something like 4096.<br><br> After you click apply, Windows will create shortcuts which you can use<br> to start and stop the container.<br><br>(3) The 'localhost' machine isn't found. This could happen if you're behind a<br> proxy. If that's the case, make sure the proxy configuration for your<br> browser knows that you shouldn't be going through the proxy to access the<br> "localhost".<br><br> In Netscape, this is under Edit/Preferences -> Advanced/Proxies, and in<br> Internet Explorer, Tools -> Internet Options -> Connections -> LAN Settings.<br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值