天天看點

Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)

Tomcat資料源的配置(四種方式<差別webapps目錄下的Web應用和其他Web應用>)

前期工作:

1. 需要提供特定資料庫的JDBC驅動,将mysql-connector-java-5.1.37-bin.jar驅動複制到Tomcat的lib路徑下。

2. 建立資料庫資料

create database db1;
use db1;
create table stu(
id int primary key,
naem varchar(20),
age int,
score double,
birthday date
);
INSERT INTO stu VALUES(1, "張無忌", 15, 100,"1994-12-16");
           
Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)

方式一:單個應用獨享資料源(可以是Tomcat的任意web應用,包括webapps檔案下的和其他目錄下的)

在tomcat中可以在server.xml的host節點中加入Context子節點來進行項目部署,添加一個私有資料源:

Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)
<Context docBase="D:\webDemo" path="/bbb" reloadable="true"> 
    <Resource  
        name="jdbc/mysql"   
        auth="Container"   
        type="javax.sql.DataSource"  
        driverClassName ="com.mysql.jdbc.Driver"  
        url="jdbc:mysql://localhost:3306/db1"  
        username="root"  
        password="root" 
        maxActive="100"     
        maxIdle="30"      
        maxWait="100000" 			
    />  
</Context>
           

apache-tomcat-9.0.29\conf\Catalina\localhost\ddd.xml(部署web應用可以删除,為了說明不是通過自定義的web部署檔案)

<Context docBase="D:\webDemo" />
           

D:\webDemo

Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)

D:\webDemo\test.jsp 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="javax.naming.*,java.sql.*,javax.sql.*" %>
<html>
  <head>
    <title>Tomcat資料源配置</title>
  </head>
  <body>
	<%

	Context ctx;
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	try{
		ctx=new InitialContext(); 

	DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");

	conn=ds.getConnection();

	stmt=conn.createStatement();

	rs=stmt.executeQuery("select * from stu where id = 1");


	while(rs.next())
	{
		out.println(rs.getInt(1) 
			+ "\t" + rs.getString(2) + "\t" + rs.getInt(3)+ "\t" + rs.getDouble(4)+ "\t" + rs.getString(5) + "<br/>");
	}
	 }catch(Exception e){
		 e.printStackTrace();
	 }finally{
		 if (rs != null) {
			 rs.close();
		 }
		 if (stmt != null) {
			 stmt.close();
		 }
		 if (conn != null) {
			 conn.close();
		 }
	 }

	%>
  </body>
</html>
           
Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)

優點:簡單

缺點:重用性差

=======================================================================

方式二:配置全局JNDI資料源,應用到所有Tomcat下部署的應用

Tomcat的conf目錄下的context.xml檔案:

<?xml version="1.0" encoding="UTF-8"?>

<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
</Context>
           
<?xml version="1.0" encoding="UTF-8"?>
<Context>

    <WatchedResource>WEB-INF/web.xml</WatchedResource>

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

</Context>
           

在Tomcat部署下的任意Web應用下建立test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="javax.naming.*,java.sql.*,javax.sql.*" %>
<html>
  <head>
    <title>Tomcat資料源配置</title>
  </head>
  <body>
	<%

	Context ctx;
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	try{
		ctx=new InitialContext(); 

	DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");

	conn=ds.getConnection();

	stmt=conn.createStatement();

	rs=stmt.executeQuery("select * from stu where id = 1");


	while(rs.next())
	{
		out.println(rs.getInt(1) 
			+ "\t" + rs.getString(2) + "\t" + rs.getInt(3)+ "\t" + rs.getDouble(4)+ "\t" + rs.getString(5) + "<br/>");
	}
	 }catch(Exception e){
		 e.printStackTrace();
	 }finally{
		 if (rs != null) {
			 rs.close();
		 }
		 if (stmt != null) {
			 stmt.close();
		 }
		 if (conn != null) {
			 conn.close();
		 }
	 }

	%>
  </body>
</html>
           
Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)

=======================================================================

方式三:配置全局JNDI資料源,應用到所有Tomcat下部署的應用(等效方法二)

第一步, 找到Tomcat的server.xml中GlobalNamingResources節點,在節點下加一個全局資料源

Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)
<Resource  
    name="jdbc/mysql"   
    auth="Container"   
    type="javax.sql.DataSource"  
    driverClassName ="com.mysql.jdbc.Driver"  
    url="jdbc:mysql://localhost:3306/db1"  
    username="root"  
    password="root" 
    maxActive="100"     
    maxIdle="30"      
    maxWait="100000" 			
/> 
           

第二步,找到Tomcat的context.xml,在Context節點下加一個ResourceLink節點對第一步配置的資料源進行引用

Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)
<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource" />  
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>  
           

在Tomcat部署下的任意Web應用下建立test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="javax.naming.*,java.sql.*,javax.sql.*" %>
<html>
  <head>
    <title>Tomcat資料源配置</title>
  </head>
  <body>
	<%

	Context ctx;
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	try{
		ctx=new InitialContext(); 

	DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");

	conn=ds.getConnection();

	stmt=conn.createStatement();

	rs=stmt.executeQuery("select * from stu where id = 1");


	while(rs.next())
	{
		out.println(rs.getInt(1) 
			+ "\t" + rs.getString(2) + "\t" + rs.getInt(3)+ "\t" + rs.getDouble(4)+ "\t" + rs.getString(5) + "<br/>");
	}
	 }catch(Exception e){
		 e.printStackTrace();
	 }finally{
		 if (rs != null) {
			 rs.close();
		 }
		 if (stmt != null) {
			 stmt.close();
		 }
		 if (conn != null) {
			 conn.close();
		 }
	 }

	%>
  </body>
</html>
           
Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)

優點:重用性,一次性到位

缺點:沒有可控性

=======================================================================

方式四:配置全局JNDI資料源,應用到單個應用(此處隻能部署在Tomcat的webapps檔案夾下的應用)

第一步, 找到Tomcat的server.xml中GlobalNamingResources節點,在節點下加一個全局資料源

Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)
<Resource  
    name="jdbc/mysql"   
    auth="Container"   
    type="javax.sql.DataSource"  
    driverClassName ="com.mysql.jdbc.Driver"  
    url="jdbc:mysql://localhost:3306/db1"  
    username="root"  
    password="root" 
    maxActive="100"     
    maxIdle="30"      
    maxWait="100000" 			
/> 
           

第二步,找到要應用此JNDI資料源的工程Context節點,增加對全局資料源的引用ResourceLink 

在aaa目錄下建立META-INF檔案夾,并建立context.xml檔案

Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)
Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)
<?xml version="1.0" encoding="UTF-8"?>

<Context docBase="aaa" path="/aaa">

	<ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource" />

</Context>
           

Tomcat的conf/context.xml儲存最初的:

<?xml version="1.0" encoding="UTF-8"?>

<Context>

    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

</Context>
           
Tomcat——Tomcat資料源的配置(四種方式,差別webapps目錄下的Web應用和其他Web應用)

=======================================================================

參考資料:

https://blog.csdn.net/dyllove98/article/details/7706218

https://blog.csdn.net/zhanglf02/article/details/76726702