No results found

Your search did not match any results.

We suggest you try the following to help find what you’re looking for:

  • Check the spelling of your keyword search.
  • Use synonyms for the keyword you typed, for example, try “application” instead of “software.”
  • Try one of the popular searches shown below.
  • Start a new search.
Trending Questions
 

Develop Java applications with Oracle Database using JDBC

This quick start guide helps Java developers to successfully establish a connection to the Oracle Database. Follow these easy steps to get started:

Prerequisites

Step 1: Prerequisites

Step 2. Setting up the Connection String

  • Download DataSourceSample.java or UCPSample.java from Github
  • Modify the Java code and update the database credentials for your database.

    (a) Change the connect string to use your database's username, password, host name/IP address, database service name, and port number,

    For Free Cloud DB or ATP or ADW: Refer to "JDBC on Cloud" page for pre-requisites and other details.
    DB_URL = "jdbc:oracle:thin:@wallet_dbname?TNS_ADMIN=/Users/test/wallet_dbname" DB_USER="testuser" and DB_PASSWORD="your_db_password"

    For on-prem database or XE database:
    DB_URL = "jdbc:oracle:thin:@myhost:1521/myorcldbservicename" DB_USER="testuser" and DB_PASSWORD="your_db_password"

    (b) DataSourceSample.java uses HR.EMPLOYEES table. If you do not have the HR schema in your database, then you can create a simple schema using JDBCSampleData.sql or modify the tables and columns in the code to use a table that you do have access to or even use a simple query "Select sysdate from dual" for testing purposes.

Maven Project

1. Pre-reqisites: Make sure to complete Step 1 and Step 2 from Pre-requisites section

2. Setup a Maven project :

  • Create a Maven project : Use the following maven command to create a project.

    
    mvn archetype:generate -DgroupId=com.oracle.jdbctest -DartifactId=jdbc-test-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 
    
  • Edit the pom.xml and add JDBC driver as a dependency. Note: ojdbc8-production will download JDBC driver (ojdbc8.jar) along with other companion jars such as oraclepki.jar, osdt_core.jar, osdt_cert.jar etc., Refer to Maven Central Guide for more details.

    
    
    <dependencies>
      <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8-production</artifactId>
        <version>19.7.0.0</version>
        <type>pom</type>
      </dependency>
    </dependencies>
    
    
    
  • Copy the code samples to src directory: Download DataSourceSample.java or UCPSample.java from Github and copy it to src directory. Add the "package com.oracle.jdbctest" to the samples to align with its location. Also, make sure the database credentials point to your database.

    3. Execute a Maven project : Make sure you are in the directory where the pom.xml is present.

    • Clean and Compile the Java code: Use the following commands.

      mvn clean

      mvn compile

    • Run the sample Java program

      
      mvn exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass="com.oracle.jdbctest.DataSourceSample"
      
      
      mvn exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass="com.oracle.jdbctest.UCPSample"
      

Gradle Project

1. Prereqisites: Make sure to complete Step 1 and Step 2 from Prerequisites section

2. Setup a Gradle project :

  • Create a Gradle project : Follow the instructions on Gradle Guide for prerequisites and build instructions. As a first step, create a gradle project using the below command. Make sure to choose "2: application" for 'Select type of project to generate'.

    $ gradle init
  • Download DataSourceSample.java or UCPSample.java from Github and copy it to src directory. Add the "package" statement to the samples to align with its location. Also, make sure to update the samples with the database credentials to point to your database.

  • Edit the build.gradle and add the JDBC driver as a dependency along with mavenCentral() as a repository. Also, update the 'mainClassName' to UCPSample or DataSourceSample. Note: 'ojdbc8-producction' will download JDBC driver (ojdbc8.jar) along with other companion jars such as oraclepki.jar, osdt_core.jar, osdt_cert.jar etc., Refer to Maven Central Guide for more details.

    
    repositories {
       // Maven Central repository
       mavenCentral()
    }
    dependencies {
       // Get the 19.7.0.0 Oracle JDBC driver along with other companion jars
        implementation("com.oracle.database.jdbc:ojdbc8-production:19.7.0.0")
    }
    application {
        // Define the main class for the application.
        mainClassName = '{your_project_directory}.UCPSample'
    }
    
    
  • 3. Run the Gradle App : Make sure you are in the directory where build.gradle is present.

    • Compile the Java code: Use the below command.

      ./gradlew build

    • Run the sample Java program

      ./gradlew run

No Build tools (Command Line Java)

1. Prerequisites Make sure to complete Step 1 and Step 2 from Prerequisites section

2. Build and Run a Sample Java Program

    Compile the Java code for Free Cloud Database or ATP/ADW

    
     javac -classpath ./test/ojdbc8.jar:./test/oraclepki.jar:./test/osdt_core.jar:./test/osdt_cert.jar DataSourceSample.java 
    
    
    java -classpath ./test/ojdbc8.jar:./test/ucp.jar:/test/oraclepki.jar:./test/osdt_core.jar:./test/osdt_cert.jar UCPSample.java 
    

    Compile the Java code for Oracle XE Database or On-Premise Oracle Database

    
    javac -classpath ./test/ojdbc8.jar:. DataSourceSample.java 
    
    
    
    javac -classpath ./test/ojdbc8.jar:./test/ucp.jar:. UCPSample.java 
    

    Run the sample Java program with Free Cloud Database or ATP/ADW

    
    java -classpath ./test/ojdbc8.jar:./test/oraclepki.jar:./test/osdt_core.jar:./test/osdt_cert.jar:. DataSourceSample 
    
    
    java -classpath ./test/ojdbc8.jar:./test/ucp.jar:/test/oraclepki.jar:./test/osdt_core.jar:./test/osdt_cert.jar:. UCPSample 
    

    Run the sample Java program with Oracle XE Database or On-Premise Oracle Database

    
    java -classpath ./test/ojdbc8.jar:. DataSourceSample 
    
    
    java -classpath ./test/ojdbc8.jar:./test/ucp.jar:. UCPSample 
    

    1 If you have JDK7 or JDK6 then you must use ojdbc7.jar or ojdbc6.jar from 12.1.0.2 or 12.1.0.1. Use "java -version" to check the JDK version that you have installed. Use "java -jar -ojdbc8.jar" to check the JDBC driver version.

    2 ATP is Autonomous Transaction Processing and ADW is Autonomous DataWarehouse