一、用于数据库连接的术语:

    JDBC:(Java database connectivity)是基于java数据访问技术的一个API通过客户端访问服务器的数据库,是一个面向关系型数据库并提供一种方法查询和更新数据库;

    JNDI:(Java naming and directory interface)JNDI服务提供了对应用程序命名和目录功 能的一种用java程序编写的基于API的java平台;

    DataSource:是一个通过JDBC API访问关系型数据库的java对象,当与JNDI整合并在JDNI 名称服务中注册后能更好的工作;

二、tomcat连接常用数据库的操作步骤:

(1)Tomcat配置Oracle DataSource:

1、在server.xml全局文件中定义如下内容:

<GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users--> <Resource name="jdbc/tomcat7" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:test" description="test database for tomcat 7" Configuration and Deployment [ 46 ] username="admin" password="admin" maxActive="20" maxIdle="10" maxWait="-1"/> </GlobalNamingResources>

2、http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html 下载Oracle JDBC驱动程序类并放在CATALINA_HOME/lib/目录下,tomcat默认只接 受.jar结尾的类,如果是zip压缩格式需要将其重名为.jar结尾,然后放到 CATALINA_HOME/lib/目录下;

3、在应用下面的WEB-INF/web.xml文件中强制定义文档类型定义,示例如下:

<resource-ref> <description>Oracle Datasource for tomcat </description> <res-ref-name>jdbc/tomcat7 </res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>

4、开发人员在代码中引用JNDI并连接到数据库;

(2)Tomcat配置mysql DataSource:

1、在server.xml全局文件中定义如下内容:

<Resource name="jdbc/tomcat7" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="tomcatuser" password="tomcat" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/tomcat7"/>

2、在应用下面的WEB-INF/web.xml文件中强制定义文档类型定义,示例如下:

<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>Tomcat 7 test DB</description> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/tomcat7</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>

3、http://dev.mysql.com/downloads/下载MYSQL JDBC驱动程序类并放在 CATALINA_HOME/lib/目录下,tomcat默认只接受.jar结尾的类,如果是zip压缩格式需 要将其重名为.jar结尾,然后放到CATALINA_HOME/lib/目录下;

4、对连接tomcat的用户授予全部权限,格式如下:

mysql> GRANT ALL PRIVILEGES ON *.* TO tomcatuser@localhost IDENTIFIED BY 'tomcat7' WITH GRANT OPTION; mysql> create database tomcat7; mysql> use tomcat7; mysql> create table testdata ( id int not null auto_increment primary key,foo varchar(25), bar int); 注:用户密码一定不要为空,否则会造成连接tomcat认证错误;

(3)Tomcat配置Postgresql DataSource:

1、在server.xml全局文件中定义如下内容:

<Resource name="jdbc/tomcat7" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432/tomcat7" username="tomcat7" password="tomcat" maxActive="20" maxIdle="10" maxWait="-1"/>

2、http://jdbc.postgresql.org/download.html下载PostgreSQL JDBC驱动类并放在 CATALINA_HOME/lib/目录下,tomcat默认只接受.jar结尾的类,如果是zip压缩格式需 要将其重名为.jar结尾,然后放到CATALINA_HOME/lib/目录下;

3、在应用下面的WEB-INF/web.xml文件中强制定义文档类型定义,示例如下:

<resource-ref> <description>postgreSQL Tomcat datasource </description> <res-ref-name>jdbc/tomcat7 </res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>

转载自http://wjw7702.blog.51cto.com/5210820/1109263