Skip to content

Commit 15cee8e

Browse files
committed
2 parents 141b552 + bb09999 commit 15cee8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1494
-3
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Context>
2+
<Resource name="jdbc/orcljdbc_ds" auth="Container"
3+
type="javax.sql.DataSource"
4+
driverClassName="oracle.jdbc.OracleDriver"
5+
username="hr"
6+
password="hr"
7+
url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))"
8+
9+
/>
10+
11+
</Context>
12+

java/jdbc/Tomcat_Servlet/Readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Tomcat Java Servlet
2+
The Oracle JDBC drivers allow Java applications to connect and process data in the Oracle Database. **Apache Tomcat** is a Java application server for building and deploying Java Web applications. This repository has code samples for a Java Servlet that connects to the Oracle Database using the Oracle JDBC driver. We have furnished `build.xml` to compile the servlet and the `Readme.md` that has instructions to compile and deploy this servlet on Tomcat. If you have subscribed to any Oracle Database Service on Cloud such as DBCS, EECS, BMCS etc., follow these instructions to verify the database connectivity with Tomcat.
3+
4+
# What you need to install?
5+
6+
* **Apache Tomcat**: Download and install [Apache Tomcat](https://tomcat.apache.org/)
7+
* **Apache Ant**: Make sure you have [Apache ANT](http://ant.apache.org/) to compile the source code
8+
* **JDBC driver**: Download the latest JDBC driver [ojdbc8.jar from OTN](http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html)
9+
* **Oracle Database**: You need to have a working Oracle Database with the credentials to verify the successful connection. Make sure either you have subscribed to Oracle Database Service on Cloud (DBCS, EECS, BMCS, ExaCS) or installed an Oracle Database on premise.
10+
11+
# Steps to compile the Java Servlet
12+
13+
* **Update build.xml**: Download the `build.xml` present in this repository. Update TOMCAT_HOME to point to the location where Tomcat is installed.
14+
* **Create a Resource in context.xml**: Download the `context.xml` present in `META-INF` folder. Update the database URL, username, and password to point to your Oracle Database. Let us name this datasource as `orcljdbc_ds`
15+
* **Update JDBCSample_Servlet**: Download the `JDBCSample_Servlet` from this repository. Update the method `getDataSource()` to use the correct Oracle datasource name. E.g.,`orcljdbc_ds`
16+
* **JDBC driver**: Place the downloaded JDBC driver ojdbc8.jar in `WEB-INF/lib` folder.
17+
* **Create the war file**: Go to the location where the `build.xml` is located. Execute the command `ant` that will compile and also create the `JDBCSample.war` file in the `dist` folder.
18+
19+
# Steps to deploy and run the Java Servlet
20+
21+
* **Deploy the WAR file**: Copy the `JDBCSample.war` file to TOMCAT_HOME/webapps/ and Start the Tomcat Server
22+
* **Invoke the Servlet**: Invoke the servlet at `https://localhost:8080/JDBCSample/JDBCSample_Servlet`
23+
* **Check the Results**: Check the results on the page and make sure that it prints driver information retrieved from the Oracle database.
24+
25+
26+
# Other Resources
27+
28+
* [Connecting Java Applications to Database Cloud Services](https://blogs.oracle.com/dev2dev/connecting-java-applications-to-database-cloud-services)
29+
* [Using Java Containers with Exadata Express Cloud Service (EECS)](http://www.oracle.com/technetwork/database/application-development/jdbc/jdbc-eecontainers-cloud.html#tomcat)

java/jdbc/Tomcat_Servlet/build.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project name="JDBCSampleDemo" default="all">
2+
<property name="classes" value="WEB-INF/classes" />
3+
<property name="src" value="src" />
4+
<property name="dist" value="dist" />
5+
<property name="TOMCAT_HOME" value="/Users/test/apache-tomcat-9.0.0.M17" />
6+
7+
<path id="JDBCSampleDemo_classpath">
8+
<pathelement location="${classes}"/>
9+
<fileset dir="${TOMCAT_HOME}/lib" includes="*.jar" />
10+
<fileset dir="WEB-INF/lib" includes="*.jar" />
11+
</path>
12+
13+
<target name="displayMessage">
14+
<echo message="-------------------------------------------------"/>
15+
<echo message=" BUILDING JDBC Sample Demo "/>
16+
<echo message="-------------------------------------------------"/>
17+
</target>
18+
19+
<target name="clean">
20+
<delete dir="${classes}"/>
21+
<delete dir="${dist}" />
22+
</target>
23+
24+
<target name="init">
25+
<mkdir dir="${classes}"/>
26+
</target>
27+
28+
29+
<target name="compile" depends="init">
30+
<javac srcdir="${src}"
31+
includeantruntime="false"
32+
destdir="${classes}"
33+
debug="on">
34+
<classpath refid="JDBCSampleDemo_classpath"/>
35+
</javac>
36+
</target>
37+
38+
39+
<target name="displayMessage2">
40+
<echo message="--------------------------------------------------"/>
41+
<echo message=" CREATING a WAR FILE "/>
42+
<echo message="--------------------------------------------------"/>
43+
</target>
44+
45+
<target name="prepare_war" depends="compile">
46+
<mkdir dir="${dist}" />
47+
<jar destfile="${dist}/JDBCSample.war" basedir="."/>
48+
</target>
49+
50+
<target name="all" depends="displayMessage, clean, init,compile,displayMessage2 ,prepare_war"></target>
51+
</project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import java.io.IOException;
2+
import java.io.PrintWriter;
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
import java.sql.Statement;
7+
import javax.sql.DataSource;
8+
import java.sql.DatabaseMetaData;
9+
10+
import javax.naming.Context;
11+
import javax.naming.InitialContext;
12+
import javax.naming.NamingException;
13+
import javax.servlet.ServletException;
14+
import javax.servlet.annotation.WebServlet;
15+
import javax.servlet.http.HttpServlet;
16+
import javax.servlet.http.HttpServletRequest;
17+
import javax.servlet.http.HttpServletResponse;
18+
19+
/**
20+
* Servlet implementation class JDBCSample_Servlet
21+
*/
22+
@WebServlet("/JDBCSample_Servlet")
23+
public class JDBCSample_Servlet extends HttpServlet {
24+
private static final long serialVersionUID = 1L;
25+
/**
26+
* @see HttpServlet#HttpServlet()
27+
*/
28+
public JDBCSample_Servlet() {
29+
super();
30+
}
31+
32+
/**
33+
* Method to get a connection to the Oracle Database and perform few
34+
* database operations and display the results on a web page.
35+
*/
36+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
37+
throws ServletException, IOException {
38+
PrintWriter out = response.getWriter();
39+
out.print("Sample JDBC Servlet");
40+
try {
41+
// Get a context for the JNDI look up
42+
DataSource ds = getDataSource();
43+
// With AutoCloseable, the connection is closed automatically.
44+
try (Connection connection = ds.getConnection()) {
45+
for (int i=0; i<10; i++) {
46+
out.println("The database user is"
47+
+ executeBusinessLogicOnDatabase(connection, out));
48+
}
49+
out.print("\n Sample JDBC Servlet Request was successful");
50+
response.setStatus(200);
51+
}
52+
} catch (Exception e) {
53+
response.setStatus(500);
54+
response.setHeader("Exception", e.toString());
55+
out.print("\n Web Request failed");
56+
out.print("\n "+e.toString());
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
/*
62+
* Method to create a datasource after the JNDI lookup
63+
*/
64+
65+
private DataSource getDataSource() throws NamingException {
66+
Context ctx;
67+
ctx = new InitialContext();
68+
Context envContext = (Context) ctx.lookup("java:/comp/env");
69+
// Look up a data source
70+
javax.sql.DataSource ds
71+
= (javax.sql.DataSource) envContext.lookup ("jdbc/orcljdbc_ds");
72+
return ds;
73+
}
74+
75+
/*
76+
* Method to showcase database operations using the database connection
77+
*/
78+
private String executeBusinessLogicOnDatabase(Connection conn,
79+
PrintWriter out) throws SQLException {
80+
// Get the JDBC driver name and version
81+
DatabaseMetaData dbmd = conn.getMetaData();
82+
out.println("Driver Name: " + dbmd.getDriverName());
83+
out.println("Driver Version: " + dbmd.getDriverVersion());
84+
85+
String user = null;
86+
String query = "select user from dual";
87+
Statement stmt = conn.createStatement();
88+
ResultSet rs = stmt.executeQuery(query);
89+
while (rs.next()) {
90+
user = rs.getString(1);
91+
}
92+
stmt.close();
93+
rs.close();
94+
return user;
95+
}
96+
97+
public void destroy() { }
98+
99+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# WebLogicServer Java Servlet
2+
The Oracle JDBC drivers allow Java applications to connect and process data in the Oracle Database. Oracle WebLogic Server is the application server for building and deploying enterprise Java EE applications. This repository has code samples for a Java Servlet that connects to the Oracle Database using the Oracle JDBC driver. We have furnished `build.xml` to compile the servlet and the `Readme.md` that has instructions to compile and deploy this servlet on WebLogic Server. If you have subscribed to any Oracle Database Service on Cloud such as DBCS, EECS, BMCS etc., follow these instructions to verify the connectivity with WebLogic Server.
3+
4+
# What you need to install?
5+
6+
* **Web Logic Server v12.2.1.2**: Download and install the [WebLogic Server v12.2.1.2](http://www.oracle.com/technetwork/middleware/weblogic/downloads/index.html)
7+
* **Apache Ant**: Make sure you have [Apache ANT](http://ant.apache.org/) to compile the source code
8+
* **JDBC driver**: You can choose to use the JDBC driver (ojdbc7.jar) that is shipped with WebLogicServer v 12.2.1.2 or you can download and use the latest [ojdbc8.jar from OTN](http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html)
9+
* **Oracle Database**: You need to have a working Oracle Database with the credentials to verify the successful connection. Make sure either you have subscribed to Oracle Database Service on Cloud (DBCS, EECS, BMCS, ExaCS) or installed an Oracle Database on premise.
10+
11+
# Steps to compile the Java Servlet
12+
13+
* **Update build.xml**: Download the `build.xml` present in this repository. Update WLS_HOME to point to the location where WebLogic Server is installed.
14+
* **Create a Datasource in WebLogicServer**: Create an Oracle Datasource through the admin console. Refer to the blog for more details ["Create and Deploy a Java Servlet using WebLogic Server"](https://blogs.oracle.com/dev2dev/create-and-deploy-a-java-servlet-using-weblogic-server-wls). Let us name this datasource as `orcljdbc_ds`
15+
* **Update JDBCSample_Servlet**: Download the `JDBCSample_Servlet` from this repository and Update the method `getDataSource()` to use the correct Oracle datasource name. E.g.,`orcljdbc_ds` created through admin console.
16+
* **Create the war file**: Go to the location where the `build.xml` is located. Execute the command `ant` that will compile and also create the `JDBCSample.war` file in the `dist` folder.
17+
18+
# Steps to deploy and run the Java Servlet
19+
20+
* **Deploy the WAR file**: Start the WebLogic Server and open the admin console. Follow the steps in this [blog](https://blogs.oracle.com/dev2dev/create-and-deploy-a-java-servlet-using-weblogic-server-wls#step8) to deploy `JDBCSample.war`
21+
* **Invoke the Servlet**: Invoke the servlet at `https://localhost:7001/JDBCSample/JDBCSample_Servlet`
22+
* **Check the Results**: Check the results on the page and make sure that it prints driver information retrieved from the Oracle database.
23+
24+
# Other Resources
25+
26+
* [Create and Deploy a Java Servlet using WebLogic Server](https://blogs.oracle.com/dev2dev/create-and-deploy-a-java-servlet-using-weblogic-server-wls)
27+
* [How to use the latest ojdbc8.jar in WebLogic Server?](http://www.oracle.com/technetwork/database/application-development/jdbc/jdbc-eecontainers-cloud.html#wls)
28+
* [Using Java Containers with Exadata Express Cloud Service (EECS)](http://www.oracle.com/technetwork/database/application-development/jdbc/jdbc-eecontainers-cloud.html#wls)
29+
30+
31+
32+
33+
34+
35+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project name="JDBCSampleDemo" default="all">
2+
<property name="classes" value="WEB-INF/classes" />
3+
<property name="src" value="src" />
4+
<property name="dist" value="dist" />
5+
<property name="WLS_HOME" value="/Users/test/wls12212/wlserver" />
6+
7+
<path id="JDBCSampleDemo_classpath">
8+
<pathelement location="${classes}"/>
9+
<fileset dir="${WLS_HOME}/server/lib" includes="*.jar" />
10+
<fileset dir="WEB-INF/lib" includes="*.jar" />
11+
</path>
12+
13+
<target name="displayMessage">
14+
<echo message="-------------------------------------------------"/>
15+
<echo message=" BUILDING JDBC Sample Demo "/>
16+
<echo message="-------------------------------------------------"/>
17+
</target>
18+
19+
<target name="clean">
20+
<delete dir="${classes}"/>
21+
<delete dir="${dist}" />
22+
</target>
23+
24+
<target name="init">
25+
<mkdir dir="${classes}"/>
26+
</target>
27+
28+
29+
<target name="compile" depends="init">
30+
<javac srcdir="${src}"
31+
includeantruntime="false"
32+
destdir="${classes}"
33+
debug="on">
34+
<classpath refid="JDBCSampleDemo_classpath"/>
35+
</javac>
36+
</target>
37+
38+
39+
<target name="displayMessage2">
40+
<echo message="--------------------------------------------------"/>
41+
<echo message=" CREATING a WAR FILE "/>
42+
<echo message="--------------------------------------------------"/>
43+
</target>
44+
45+
<target name="prepare_war" depends="compile">
46+
<mkdir dir="${dist}" />
47+
<jar destfile="${dist}/JDBCSample.war" basedir="."/>
48+
</target>
49+
50+
<target name="all" depends="displayMessage, clean, init,compile,displayMessage2 ,prepare_war"></target>
51+
</project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.io.IOException;
2+
import java.io.PrintWriter;
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
import java.sql.Statement;
7+
import java.sql.DatabaseMetaData;
8+
import javax.sql.DataSource;
9+
10+
import javax.naming.Context;
11+
import javax.naming.InitialContext;
12+
import javax.naming.NamingException;
13+
import javax.servlet.ServletException;
14+
import javax.servlet.annotation.WebServlet;
15+
import javax.servlet.http.HttpServlet;
16+
import javax.servlet.http.HttpServletRequest;
17+
import javax.servlet.http.HttpServletResponse;
18+
19+
20+
21+
/**
22+
* Servlet implementation class JDBCSample_Servlet
23+
*/
24+
@WebServlet("/JDBCSample_Servlet")
25+
public class JDBCSample_Servlet extends HttpServlet {
26+
private static final long serialVersionUID = 1L;
27+
public JDBCSample_Servlet() {
28+
super();
29+
}
30+
/*
31+
* Method to establish the connection to the Database and
32+
* perform some database operations.
33+
*/
34+
protected void doGet(HttpServletRequest request,
35+
HttpServletResponse response) throws ServletException,IOException {
36+
PrintWriter out = response.getWriter();
37+
out.println("Sample JDBC Servlet");
38+
try {
39+
// Get a context for the JNDI look up
40+
DataSource ds = getDataSource();
41+
out.println("Data source is " + ds );
42+
// With AutoCloseable, the connection is closed automatically.
43+
try ( Connection connection = ds.getConnection()) {
44+
out.println("Connection obtained " + connection);
45+
for (int i=0; i<10; i++) {
46+
out.println("The database user is" +
47+
executeBusinessLogicOnDatabase(connection, out));
48+
}
49+
out.print("\n Sample JDBC Servlet Request was successful");
50+
response.setStatus(200);
51+
}
52+
} catch (Exception e) {
53+
response.setStatus(500);
54+
response.setHeader("Exception", e.toString());
55+
out.print("\n Web Request failed");
56+
out.print("\n "+e.toString());
57+
e.printStackTrace();
58+
}
59+
}
60+
/*
61+
* Method that creates the datasource after the JNDI lookup
62+
*/
63+
private DataSource getDataSource() throws NamingException {
64+
Context ctx = new InitialContext();
65+
// Look up for the JNDI datasource
66+
javax.sql.DataSource ds
67+
= (javax.sql.DataSource) ctx.lookup ("orcljdbc_ds");
68+
return ds;
69+
}
70+
/*
71+
* Method to show case database operations using the database connection
72+
*/
73+
private String executeBusinessLogicOnDatabase(Connection conn, PrintWriter out) throws SQLException {
74+
// Get the JDBC driver name and version
75+
DatabaseMetaData dbmd = conn.getMetaData();
76+
out.println("Driver Name: " + dbmd.getDriverName());
77+
out.println("Driver Version: " + dbmd.getDriverVersion());
78+
79+
String user = null;
80+
String query = "select user from dual";
81+
Statement stmt = conn.createStatement();
82+
ResultSet rs = stmt.executeQuery(query);
83+
while (rs.next()) {
84+
user = rs.getString(1);
85+
}
86+
stmt.close();
87+
rs.close();
88+
return user;
89+
}
90+
91+
public void destroy() {}
92+
93+
}

java/ojvm/SODA.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The furnished testSODA.java on this page already has the change
4242
call dbms_java.set_output(2000);
4343
call testSODA();
4444

45-
The furnished testSODA.sql performs the steps (v) and (vi).
45+
The furnished testSODA.sql performs the steps (iv) and (v).
4646

4747
From a database SQLPLUS session, issue the following call
4848

java/ucp/ConnectionManagementSamples/UCPSample.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ The code sample demonstrates Universal Connection Pool (UCP) as a client
3030
import oracle.ucp.jdbc.PoolDataSource;
3131

3232
public class UCPSample {
33-
final static String DB_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))";
33+
final static String DB_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))";
34+
// Use TNS alias when using tnsnames.ora. Use it while connecting to the database service on cloud.
35+
// final static String DB_URL= "jdbc:oracle:thin:@orcldbaccess";
3436
final static String DB_USER = "hr";
3537
final static String DB_PASSWORD = "hr";
3638
final static String CONN_FACTORY_CLASS_NAME = "oracle.jdbc.pool.OracleDataSource";

0 commit comments

Comments
 (0)