Here is the procedure, how to configure Oracle datasource in Tomcat and lookup the same.
1. Get Oracle driver
Download Oracle driver from here and copy it to $TOMCAT\lib folder
2. Create META-INF/context.xml and add following entry.
<?xml version="1.0" encoding="windows-1252" ?>
<Context override="true">
<Resource name="jdbc/hr" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="hr" password="welcome1"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl" />
</Context>
3. web.xml configuration
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<resource-ref>
<description>Oracle Datasource</description>
<res-ref-name>jdbc/hr</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
4. Lookup datasource via context lookup service
Below class lookup datasource and established connection with Oracle DB
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class GetConnection {
private static DataSource ds;
public static DataSource getDS() {
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/hr");
} catch (NamingException e) {
e.printStackTrace();
}
return ds;
}
public static void main(String[] args) {
try {
DataSource ds = GetConnection.getDS();
Connection connection = ds.getConnection();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
1. Get Oracle driver
Download Oracle driver from here and copy it to $TOMCAT\lib folder
2. Create META-INF/context.xml and add following entry.
<?xml version="1.0" encoding="windows-1252" ?>
<Context override="true">
<Resource name="jdbc/hr" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="hr" password="welcome1"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl" />
</Context>
3. web.xml configuration
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<resource-ref>
<description>Oracle Datasource</description>
<res-ref-name>jdbc/hr</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
4. Lookup datasource via context lookup service
Below class lookup datasource and established connection with Oracle DB
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class GetConnection {
private static DataSource ds;
public static DataSource getDS() {
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/hr");
} catch (NamingException e) {
e.printStackTrace();
}
return ds;
}
public static void main(String[] args) {
try {
DataSource ds = GetConnection.getDS();
Connection connection = ds.getConnection();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
No comments:
Post a Comment