杰瑞科技汇

Java如何连接Oracle数据库的jar包怎么配置?

Of course. The term "Java Oracle jar" can refer to a few different things, but it generally means a Java Archive (.jar) file that is related to Oracle in some way. Let's break down the most common scenarios.

Java如何连接Oracle数据库的jar包怎么配置?-图1
(图片来源网络,侵删)

Scenario 1: The Oracle JDBC Driver (Most Common)

This is the most frequent reason someone searches for a "Java Oracle jar". You need this JAR file to connect a Java application to an Oracle Database.

What is it?

The Oracle JDBC driver is a library that allows Java applications to communicate with an Oracle Database using the standard JDBC (Java Database Connectivity) API.

How to Get the Oracle JDBC Driver JAR

Important: Oracle has changed its distribution model. You now need an Oracle account (free) to download the driver.

Step-by-Step Guide:

Java如何连接Oracle数据库的jar包怎么配置?-图2
(图片来源网络,侵删)
  1. Go to the Oracle Website: Navigate to the Oracle Database JDBC-ODBC Drivers page: https://www.oracle.com/database/technologies/appdev/jdbc/downloads/

  2. Accept the License: You will be asked to review and accept the Oracle Technology Network License Agreement for Oracle Java SE. Click "Accept License Agreement".

  3. Download the JAR:

    • Look for the "Download" link for the JDBC driver. The file is often named something like ojdbc8.jar or ojdbc11.jar.
    • The number in the name corresponds to the Java version it's primarily designed for (e.g., ojdbc8.jar for Java 8, ojdbc11.jar for Java 11/17).
    • Choose the right file. For modern projects, ojdbc11.jar is the most common choice.
  4. Add the JAR to Your Project: Once you have downloaded the .jar file, you need to add it to your project's classpath.

    Java如何连接Oracle数据库的jar包怎么配置?-图3
    (图片来源网络,侵删)
    • For Maven Projects (Recommended): The easiest way is to use the com.oracle.database.jdbc groupId. Add this dependency to your pom.xml:

      <dependency>
          <groupId>com.oracle.database.jdbc</groupId>
          <artifactId>ojdbc11</artifactId>
          <version>21.9.0.0</version> <!-- Check for the latest version -->
      </dependency>

      Maven will automatically download the JAR from the Oracle Maven repository.

    • For Gradle Projects: Add this to your build.gradle file:

      implementation 'com.oracle.database.jdbc:ojdbc11:21.9.0.0' // Check for the latest version
    • For Manual Projects (e.g., using the command line): You need to specify the path to the downloaded JAR when compiling and running your code.

      Compile:

      javac -cp ".:/path/to/ojdbc11.jar" YourDatabaseClass.java

      (Use instead of on Windows)

      Run:

      java -cp ".:/path/to/ojdbc11.jar" YourDatabaseClass

      (Use instead of on Windows)


Scenario 2: Oracle's Core Java Libraries (JDK)

This refers to the JAR files that make up the Java Development Kit (JDK) itself, which is provided by Oracle.

What is it?

The Oracle JDK is the official implementation of the Java Platform, Standard Edition (Java SE). It contains the compiler (javac), the runtime environment (java), and a vast set of libraries packaged as JAR files (e.g., rt.jar, java.base.jar).

How to Get It

  1. Go to the Oracle Java SE Downloads page: https://www.oracle.com/java/technologies/downloads/

  2. Download the JDK: Choose the version you need (e.g., JDK 21, JDK 17) and download the installer for your operating system.

  3. Where are the JARs? After installation, the JAR files are located in the jre/lib or lib directory of your JDK installation path. You don't typically need to interact with these JARs directly, as the java and javac tools know where to find them.


Scenario 3: Oracle WebLogic Server or Other Oracle Products

This refers to JAR files that are part of larger Oracle middleware or application server products.

What is it?

If you are using a product like Oracle WebLogic Server, Oracle Fusion Middleware, or Oracle ADF (Application Development Framework), these products come with their own set of JAR files that contain their specific APIs and functionality.

How to Get It

  • These JARs are bundled with the product installation. You don't download them separately.
  • When you develop an application for one of these platforms, you typically add the product's libraries to your project's classpath through an IDE (like IntelliJ or Eclipse) or a build tool (like Maven or Gradle) using a specific "Bill of Materials" (BOM) or dependency provided by Oracle for that product.

Summary Table

Scenario What it is How to Get the JAR Common Use Case
Oracle JDBC Driver Library to connect Java to an Oracle Database. Download from the Oracle JDBC site after accepting the license. Connecting a Spring Boot, Java EE, or standalone Java app to an Oracle DB.
Oracle JDK JARs Core Java libraries that come with the official JDK. Download the full Oracle JDK package from Oracle Java SE Downloads. The foundation for all Java development. You get these by installing the JDK.
Oracle Product JARs Libraries for specific Oracle middleware (e.g., WebLogic). Included in the installation of the Oracle product itself. Developing applications that run on or are built for Oracle's middleware platforms.

Simple Code Example (Using JDBC)

Here’s a basic example of how you would use the ojdbc11.jar to connect to a database.

Create a file OracleConnectionExample.java:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleConnectionExample {
    // Replace with your database connection details
    static final String DB_URL = "jdbc:oracle:thin:@your_host:your_port:your_service_name";
    static final String USER = "your_username";
    static final String PASS = "your_password";
    public static void main(String[] args) {
        // The driver is loaded by the classpath, no need for Class.forName in modern JDBC drivers
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT 'Hello from Oracle!' as message FROM DUAL")) {
            // Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                String message = rs.getString("message");
                System.out.println("Database Response: " + message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Compile and Run (with the JAR in the classpath):

Assuming your JAR is in C:/drivers/ojdbc11.jar and your DB details are filled in:

# Compile the Java file
javac -cp "C:/drivers/ojdbc11.jar" OracleConnectionExample.java
# Run the compiled class
java -cp ".;C:/drivers/ojdbc11.jar" OracleConnectionExample

(Remember to use as the separator on Linux/macOS and on Windows.)

分享:
扫描分享到社交APP
上一篇
下一篇