Tomcat's JNDI settings

  • Using the database connection pool:
    Step 1: Add the required database jar package to the tomcat/lib folder;
    Step 2: Configure the context.xml in the tomcat/conf folder, and add the following Resource nodes
<!-- 属性含义:
name:指定Resource的JNDI名称
auth:指定管理Resource的Manager
type:指定Resource所属的Java类
maxActive:指定连接池中处于“活动”状态的数据库连接的最大数目
maxIdle:指定连接池中处于“空闲”状态的数据库连接的最大数目
maxWait:指定连接池中的连接处于空闲的最长时间
username:数据库登录用户名
password:数据库登录密码
driverClassName:数据库对应版本的驱动(jar包,,web应用程序不需要该jar包)
url:数据库连接
-->
<!-- oracle数据库 -->
<Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource" 
        maxActive="20" maxIdle="10" maxWait="10000" 
        username="news" password="123456" driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>

<!-- mysql-->
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
        maxActive="20" maxIdle="10" maxWait="10000" 
        username="root" password="123" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql:/127.0.0.1:3306/test"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • Step 3: Write in Java code
import javax.naming.*;
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
Connection conn = ds.getConnection();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325924724&siteId=291194637