2 Using Maven and Gradle

The following sections provide information about how to connect to Oracle Database using a Maven project or a Gradle project.

2.1 Using a Maven Project

You can establish a connection to Oracle Database using the Maven Project.

2.1.1 Setting Up a Maven Project

This section lists the steps to set up a Maven project.

Make sure to complete all the steps from the Prerequisites section.
  1. Create a Maven project.

    Download Apache Maven and set the PATH before using mvn commands. 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
  2. Copy the QuickStart.java file to the src/main/java/com/oracle/jdbctest directory.
  3. Modify the pom.xml file with the following changes:
    • Add Oracle JDBC driver as a dependency.

      Note:

      ojdbc8-production downloads Oracle JDBC driver (ojdbc8.jar) along with UCP, a client side connection pool (ucp.jar). Refer to the Maven Central Guide for more details.
      <properties>
          <maven.compiler.source>11</maven.compiler.source>
          <maven.compiler.target>11</maven.compiler.target>
      </properties>
      <dependencies>
        <dependency>
          <groupId>com.oracle.database.jdbc</groupId>
          <artifactId>ojdbc8-production</artifactId>
          <version>19.9.0.0</version>
          <type>pom</type>
        </dependency>
      </dependencies>

2.1.2 Building and Running a Sample Java Program

This section lists the steps to build and run a sample Java program.

Make sure that you are in the directory where the pom.xml file is present.
  1. Clean and compile the Java code.

    Use the following commands:

    mvn clean
    mvn compile
  2. Run the sample Java program:
    mvn exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass="com.oracle.jdbctest.QuickStart"

Sample Output:

You will see the queried rows returned from the new table todoitem and a message Congratulations! You have successfully used Oracle Database as shown in the following screen:
Sample Output

2.2 Using a Gradle Project

You can establish a connection to Oracle Database using the Gradle project.

2.2.1 Setting Up a Gradle Project

This section lists the steps to set up a Gradle project.

Make sure to complete all the steps from the Prerequisites section.
  1. Create a Gradle project.

    Follow instructions from the Gradle Guide for Gradle download and build instructions and set the PATH before using Gradle commands. As a first step, create a Gradle project using the following command. Make sure to choose 2:application for Select type of project to generate. Also, for Source package (default:temp):, use com.oracle.jdbctest.

    gradle init
  2. Copy the QuickStart.java file to the src/main/java/com/oracle/jdbctest directory.
  3. Modify the build.gradle file with the following changes:
    • Add mavenCentral()as a repository.
    • Add the Oracle JDBC driver as a dependency.

      Note:

      ojdbc8-production downloads Oracle JDBC driver (ojdbc8.jar) along with UCP as a client side connection pool (ucp.jar). Refer to the Maven Central Guide for more details.
    • Update the mainClassName to QuickStart.
    repositories { 
      // Maven Central
       mavenCentral()
     } 
    dependencies { 
      // Get the 21.1.0.0 Oracle JDBC driver along with other companion jars
      implementation("com.oracle.database.jdbc:ojdbc8-production:19.9.0.0")
     }
    application { 
      // Define the main class for the application
      mainClassName ='{your_project_directory}.QuickStart' 
    }

2.2.2 Building and Running the Gradle Application

This section lists the steps to run the Gradle application.

Make sure you are in the directory where the build.gradle file is present.
  1. Compile the Jave code using the following command:
    ./gradlew build
  2. Run the sample Java Program.
    ./gradlew run
Sample Output: You will see the queried rows returned from the new table todoitem and a message Congratulations! You have successfully used Oracle Database as shown in the following screen.
Sample Output