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
<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" />
<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();