歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> tomcat連接常用數據庫的用法

tomcat連接常用數據庫的用法

日期:2017/2/27 16:02:30   编辑:Linux教程
一、用於數據庫連接的術語: 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> 

Copyright © Linux教程網 All Rights Reserved