Running java with oracle

Running java with oracle

Oracle provides enterprise application developers an end-to-end Java solution for creating, deploying, and managing Java applications. The total solution consists of client-side and server-side programmatic interfaces, tools to support Java development, and a JVM integrated with Oracle Database. All these products are fully compatible with Java standards. This section covers the following topics:

1.6.1 Java in Database Application Development

The most important features of Java in database application development are:

  • Designing data-bound procedures and functions using Java SE APIs and JDBC.
  • Extending the reach and capabilities of the database with standard and third-party Java libraries. For example, accessing third-party databases using their drivers in the database and accessing Hadoop/HDFS.
  • Providing flexible partitioning of Java2 Platform, Standard Edition (J2SE) applications for symmetric data access at the JDBC level.
  • Bridging SQL and the Java2 Platform, Enterprise Edition (J2EE) world by:
    • Calling out Web components, such as JSP and servlet
    • Bridging SQL and Web Services using Web Service Callouts

    1.6.2 Java Programming Environment Usage

    In addition to the Oracle JVM, the Java programming environment provides:

    • Java stored procedures as the Java equivalent and companion for PL/SQL. Java stored procedures are tightly integrated with PL/SQL. You can call Java stored procedures from PL/SQL packages and PL/SQL procedures from Java stored procedures.
    • The JDBC and SQLJ programming interfaces for accessing SQL data.
    • Tools and scripts that assist in developing, loading, and managing classes.

    The following table helps you decide when to use which Java API:

    To have a Java procedure called from SQL, such as a trigger.

    To call a static, simple SQL statement from a known table with known column names from a Java object.

    To call dynamic, complex SQL statements from a Java object.

    1.6.3 Java Stored Procedures

    Java stored procedures are Java programs written and deployed on a server and run from the server, exactly like a PL/SQL stored procedure. You invoke it directly with products like SQL*Plus, or indirectly with a trigger. You can access it from any Oracle Net client, such as OCI and PRO*, or JDBC or SQLJ.

    In addition, you can use Java to develop powerful, server-side programs, which can be independent of PL/SQL. Oracle Database provides a complete implementation of the standard Java programming language and a fully compliant JVM.

    Related Topics

    1.6.4 PL/SQL Integration and Oracle RDBMS Functionality

    You can call existing PL/SQL programs from Java and Java programs from PL/SQL. This solution protects and leverages your PL/SQL and Java code and opens up the advantages and opportunities of Java-based Internet computing.

    Oracle Database offers two different Java APIs for accessing SQL data, JDBC and SQLJ. Both these APIs are available on the client, and the JDBC API is also available on the server. As a result, you can deploy your applications on the client and server.

    The following topics introduce the Java APIs provided by Oracle Database:

    1.6.4.1 JDBC Drivers

    JDBC is a database access protocol that enables you to connect to a database and run SQL statements and queries to the database. The core Java class libraries provide the following JDBC APIs: java.sql and javax.sql . However, JDBC is designed to enable vendors to supply drivers that offer the necessary specialization for a particular database. Oracle provides the following distinct JDBC drivers:

    You can use the JDBC Thin driver to write pure Java applications and applets that access Oracle SQL data. The JDBC Thin driver is especially well-suited for Web-based applications and applets, because you can dynamically download it from a Web page, similar to any other Java applet.

    The JDBC OCI driver accesses Oracle-specific native code, that is, non-Java code, and libraries on the client or middle tier, providing performance boost compared to the JDBC Thin driver, at the cost of significantly larger size and client-side installation.

    JDBC server-side internal driver

    Oracle Database uses the server-side internal driver when the Java code runs on the server. It allows Java applications running in the Oracle JVM on the server to access locally defined data, that is, data on the same system and in the same process, with JDBC. It provides a performance boost, because of its ability to use the underlying Oracle RDBMS libraries directly, without the overhead of an intervening network connection between the Java code and SQL data. By supporting the same Java-SQL interface on the server, Oracle Database does not require you to rework code when deploying it.

    Related Topics

    Источник

    Running java with oracle

    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

    • Get access to the Oracle Database instance. You can either Sign Up for the Oracle Cloud Free Tier or Oracle Database in the Cloud or Install an Oracle Database or Download Free Oracle Database XE
    • Install JDK8 1 or other higher JDK versions
    • Get the latest JDBC driver from Central Maven or Download the latest 19.8 JDBC driver (ojdbc8.jar) and place it at a location accessible to your Java program. While connecting to Free Cloud DB or ATP/ADW 2 , make sure to have oraclepki.jar, osdt_core.jar, and osdt_cert.jar in the classpath.

    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
        com.oracle.database.jdbc ojdbc8-production 19.7.0.0 pom     
    • 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 = '.UCPSample' > 
    • 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

    Источник

    Читайте также:  Python django open file
Оцените статью