Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9fdfd7f
Checking in the Servlet and WAR files for WLS
Jun 28, 2017
c419450
Checking in Tomcat files
Jun 28, 2017
6e05237
Checking in the Readme.md files
Jul 10, 2017
322cee4
Update Readme.md
nsundara Jul 18, 2017
2551081
Update Readme.md
nsundara Jul 18, 2017
c7aa2f4
Update Readme.md
nsundara Jul 18, 2017
b2ae12f
Update Readme.md
nsundara Jul 18, 2017
8d6519f
Update Readme.md
nsundara Jul 18, 2017
444d8bd
Update Readme.md
nsundara Jul 18, 2017
15eea82
Update Readme.md
nsundara Jul 18, 2017
7bdc3ac
Update Readme.md
nsundara Jul 18, 2017
e7e1b6f
Update Readme.md
nsundara Jul 18, 2017
4076298
Update Readme.md
nsundara Jul 18, 2017
cf9251c
Update Readme.md
nsundara Jul 18, 2017
8ba8ac6
Checking in files for Tomcat
Jul 18, 2017
000073b
Merge branch 'wls_servlet' of https://github.com/oracle/oracle-db-exa…
Jul 18, 2017
38d2061
Update Readme.md
nsundara Jul 21, 2017
72e0b92
Update Readme.md
nsundara Jul 21, 2017
1be975f
Update Readme.md
nsundara Jul 21, 2017
5b29e1f
Update Readme.md
nsundara Jul 21, 2017
578340d
Merge branch 'wls_servlet' of https://github.com/oracle/oracle-db-exa…
Jul 21, 2017
f0c767b
Checking in the changes
Jul 21, 2017
25f2292
Update Readme.md
nsundara Jul 21, 2017
727fc70
Update Readme.md
nsundara Jul 21, 2017
8775d43
Update Readme.md
nsundara Jul 21, 2017
3b1f773
Update Readme.md
nsundara Jul 21, 2017
71597e1
Update Readme.md
nsundara Jul 21, 2017
2c99c9a
Modified Tomcat Servlet
Jul 21, 2017
5f0e6e6
Merge branch 'wls_servlet' of https://github.com/oracle/oracle-db-exa…
Jul 21, 2017
fd0bcce
Update Readme.md
Kuassim Jul 24, 2017
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions java/jdbc/Tomcat_Servlet/META-INF/context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Context>
<Resource name="jdbc/orcljdbc_ds" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
username="hr"
password="hr"
url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))"

/>

</Context>

29 changes: 29 additions & 0 deletions java/jdbc/Tomcat_Servlet/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Tomcat Java Servlet
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.

# What you need to install?

* **Apache Tomcat**: Download and install [Apache Tomcat](https://tomcat.apache.org/)
* **Apache Ant**: Make sure you have [Apache ANT](http://ant.apache.org/) to compile the source code
* **JDBC driver**: Download the latest JDBC driver [ojdbc8.jar from OTN](http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html)
* **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.

# Steps to compile the Java Servlet

* **Update build.xml**: Download the `build.xml` present in this repository. Update TOMCAT_HOME to point to the location where Tomcat is installed.
* **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`
* **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`
* **JDBC driver**: Place the downloaded JDBC driver ojdbc8.jar in `WEB-INF/lib` folder.
* **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.

# Steps to deploy and run the Java Servlet

* **Deploy the WAR file**: Copy the `JDBCSample.war` file to TOMCAT_HOME/webapps/ and Start the Tomcat Server
* **Invoke the Servlet**: Invoke the servlet at `https://localhost:8080/JDBCSample/JDBCSample_Servlet`
* **Check the Results**: Check the results on the page and make sure that it prints driver information retrieved from the Oracle database.


# Other Resources

* [Connecting Java Applications to Database Cloud Services](https://blogs.oracle.com/dev2dev/connecting-java-applications-to-database-cloud-services)
* [Using Java Containers with Exadata Express Cloud Service (EECS)](http://www.oracle.com/technetwork/database/application-development/jdbc/jdbc-eecontainers-cloud.html#tomcat)
51 changes: 51 additions & 0 deletions java/jdbc/Tomcat_Servlet/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project name="JDBCSampleDemo" default="all">
<property name="classes" value="WEB-INF/classes" />
<property name="src" value="src" />
<property name="dist" value="dist" />
<property name="TOMCAT_HOME" value="/Users/test/apache-tomcat-9.0.0.M17" />

<path id="JDBCSampleDemo_classpath">
<pathelement location="${classes}"/>
<fileset dir="${TOMCAT_HOME}/lib" includes="*.jar" />
<fileset dir="WEB-INF/lib" includes="*.jar" />
</path>

<target name="displayMessage">
<echo message="-------------------------------------------------"/>
<echo message=" BUILDING JDBC Sample Demo "/>
<echo message="-------------------------------------------------"/>
</target>

<target name="clean">
<delete dir="${classes}"/>
<delete dir="${dist}" />
</target>

<target name="init">
<mkdir dir="${classes}"/>
</target>


<target name="compile" depends="init">
<javac srcdir="${src}"
includeantruntime="false"
destdir="${classes}"
debug="on">
<classpath refid="JDBCSampleDemo_classpath"/>
</javac>
</target>


<target name="displayMessage2">
<echo message="--------------------------------------------------"/>
<echo message=" CREATING a WAR FILE "/>
<echo message="--------------------------------------------------"/>
</target>

<target name="prepare_war" depends="compile">
<mkdir dir="${dist}" />
<jar destfile="${dist}/JDBCSample.war" basedir="."/>
</target>

<target name="all" depends="displayMessage, clean, init,compile,displayMessage2 ,prepare_war"></target>
</project>
99 changes: 99 additions & 0 deletions java/jdbc/Tomcat_Servlet/src/JDBCSample_Servlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import java.sql.DatabaseMetaData;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class JDBCSample_Servlet
*/
@WebServlet("/JDBCSample_Servlet")
public class JDBCSample_Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JDBCSample_Servlet() {
super();
}

/**
* Method to get a connection to the Oracle Database and perform few
* database operations and display the results on a web page.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("Sample JDBC Servlet");
try {
// Get a context for the JNDI look up
DataSource ds = getDataSource();
// With AutoCloseable, the connection is closed automatically.
try (Connection connection = ds.getConnection()) {
for (int i=0; i<10; i++) {
out.println("The database user is"
+ executeBusinessLogicOnDatabase(connection, out));
}
out.print("\n Sample JDBC Servlet Request was successful");
response.setStatus(200);
}
} catch (Exception e) {
response.setStatus(500);
response.setHeader("Exception", e.toString());
out.print("\n Web Request failed");
out.print("\n "+e.toString());
e.printStackTrace();
}
}

/*
* Method to create a datasource after the JNDI lookup
*/

private DataSource getDataSource() throws NamingException {
Context ctx;
ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
// Look up a data source
javax.sql.DataSource ds
= (javax.sql.DataSource) envContext.lookup ("jdbc/orcljdbc_ds");
return ds;
}

/*
* Method to showcase database operations using the database connection
*/
private String executeBusinessLogicOnDatabase(Connection conn,
PrintWriter out) throws SQLException {
// Get the JDBC driver name and version
DatabaseMetaData dbmd = conn.getMetaData();
out.println("Driver Name: " + dbmd.getDriverName());
out.println("Driver Version: " + dbmd.getDriverVersion());

String user = null;
String query = "select user from dual";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
user = rs.getString(1);
}
stmt.close();
rs.close();
return user;
}

public void destroy() { }

}
35 changes: 35 additions & 0 deletions java/jdbc/WebLogicServer_Servlet/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# WebLogicServer Java Servlet
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.

# What you need to install?

* **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)
* **Apache Ant**: Make sure you have [Apache ANT](http://ant.apache.org/) to compile the source code
* **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)
* **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.

# Steps to compile the Java Servlet

* **Update build.xml**: Download the `build.xml` present in this repository. Update WLS_HOME to point to the location where WebLogic Server is installed.
* **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`
* **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.
* **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.

# Steps to deploy and run the Java Servlet

* **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`
* **Invoke the Servlet**: Invoke the servlet at `https://localhost:7001/JDBCSample/JDBCSample_Servlet`
* **Check the Results**: Check the results on the page and make sure that it prints driver information retrieved from the Oracle database.

# Other Resources

* [Create and Deploy a Java Servlet using WebLogic Server](https://blogs.oracle.com/dev2dev/create-and-deploy-a-java-servlet-using-weblogic-server-wls)
* [How to use the latest ojdbc8.jar in WebLogic Server?](http://www.oracle.com/technetwork/database/application-development/jdbc/jdbc-eecontainers-cloud.html#wls)
* [Using Java Containers with Exadata Express Cloud Service (EECS)](http://www.oracle.com/technetwork/database/application-development/jdbc/jdbc-eecontainers-cloud.html#wls)







51 changes: 51 additions & 0 deletions java/jdbc/WebLogicServer_Servlet/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project name="JDBCSampleDemo" default="all">
<property name="classes" value="WEB-INF/classes" />
<property name="src" value="src" />
<property name="dist" value="dist" />
<property name="WLS_HOME" value="/Users/test/wls12212/wlserver" />

<path id="JDBCSampleDemo_classpath">
<pathelement location="${classes}"/>
<fileset dir="${WLS_HOME}/server/lib" includes="*.jar" />
<fileset dir="WEB-INF/lib" includes="*.jar" />
</path>

<target name="displayMessage">
<echo message="-------------------------------------------------"/>
<echo message=" BUILDING JDBC Sample Demo "/>
<echo message="-------------------------------------------------"/>
</target>

<target name="clean">
<delete dir="${classes}"/>
<delete dir="${dist}" />
</target>

<target name="init">
<mkdir dir="${classes}"/>
</target>


<target name="compile" depends="init">
<javac srcdir="${src}"
includeantruntime="false"
destdir="${classes}"
debug="on">
<classpath refid="JDBCSampleDemo_classpath"/>
</javac>
</target>


<target name="displayMessage2">
<echo message="--------------------------------------------------"/>
<echo message=" CREATING a WAR FILE "/>
<echo message="--------------------------------------------------"/>
</target>

<target name="prepare_war" depends="compile">
<mkdir dir="${dist}" />
<jar destfile="${dist}/JDBCSample.war" basedir="."/>
</target>

<target name="all" depends="displayMessage, clean, init,compile,displayMessage2 ,prepare_war"></target>
</project>
93 changes: 93 additions & 0 deletions java/jdbc/WebLogicServer_Servlet/src/JDBCSample_Servlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DatabaseMetaData;
import javax.sql.DataSource;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
* Servlet implementation class JDBCSample_Servlet
*/
@WebServlet("/JDBCSample_Servlet")
public class JDBCSample_Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public JDBCSample_Servlet() {
super();
}
/*
* Method to establish the connection to the Database and
* perform some database operations.
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException {
PrintWriter out = response.getWriter();
out.println("Sample JDBC Servlet");
try {
// Get a context for the JNDI look up
DataSource ds = getDataSource();
out.println("Data source is " + ds );
// With AutoCloseable, the connection is closed automatically.
try ( Connection connection = ds.getConnection()) {
out.println("Connection obtained " + connection);
for (int i=0; i<10; i++) {
out.println("The database user is" +
executeBusinessLogicOnDatabase(connection, out));
}
out.print("\n Sample JDBC Servlet Request was successful");
response.setStatus(200);
}
} catch (Exception e) {
response.setStatus(500);
response.setHeader("Exception", e.toString());
out.print("\n Web Request failed");
out.print("\n "+e.toString());
e.printStackTrace();
}
}
/*
* Method that creates the datasource after the JNDI lookup
*/
private DataSource getDataSource() throws NamingException {
Context ctx = new InitialContext();
// Look up for the JNDI datasource
javax.sql.DataSource ds
= (javax.sql.DataSource) ctx.lookup ("orcljdbc_ds");
return ds;
}
/*
* Method to show case database operations using the database connection
*/
private String executeBusinessLogicOnDatabase(Connection conn, PrintWriter out) throws SQLException {
// Get the JDBC driver name and version
DatabaseMetaData dbmd = conn.getMetaData();
out.println("Driver Name: " + dbmd.getDriverName());
out.println("Driver Version: " + dbmd.getDriverVersion());

String user = null;
String query = "select user from dual";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
user = rs.getString(1);
}
stmt.close();
rs.close();
return user;
}

public void destroy() {}

}