You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Eclipse JEE 2019中连接SQL Developer数据库?求驱动与代码

Hey there! I get you're looking to connect your Eclipse JEE 2019 to an Oracle database (since SQL Developer is Oracle's go-to tool for managing their databases). Let's break this down into three simple parts: getting the right driver, setting up the connection in Eclipse, and writing the Java code to interact with the database.

Step 1: Get the Oracle JDBC Driver

You'll need the Oracle JDBC driver (officially named ojdbc) to bridge Java and your Oracle database. Make sure the driver version matches your Oracle database version:

  • Use ojdbc8 for Oracle 12c, 18c, 19c, or 21c
  • Use ojdbc6 for Oracle 11g

Option 1: Manual Download

Head to Oracle's official JDBC driver page to download the appropriate JAR file. Once downloaded, you'll add it to your Eclipse project's build path later (or use it in the database connection setup).

Option 2: Maven Dependency (If You Use Maven)

If your project uses Maven, add this dependency to your pom.xml to automatically fetch the driver:

<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>21.13.0.0</version> <!-- Use the version matching your DB -->
</dependency>
Step 2: Set Up Database Connection in Eclipse JEE 2019

Let's configure the direct connection in Eclipse's Database Development perspective:

  • Open Eclipse, go to Window > Perspective > Open Perspective > Other > Database Development to switch to the database-focused view.
  • In the Data Source Explorer panel (usually on the left), right-click Database Connections and select New.
  • From the list of database types, choose Oracle Database and click Next.
  • Select the driver you want to use (e.g., Oracle JDBC Driver). If the driver isn't listed, click New Driver Definition, then Add JAR/Zip to select the downloaded ojdbc JAR file.
  • Fill in your connection details:
    • Database URL: Use either format:
      • SID format: jdbc:oracle:thin:@<your-db-host>:<port>:<SID> (e.g., jdbc:oracle:thin:@localhost:1521:ORCL for a local Oracle DB)
      • Service name format: jdbc:oracle:thin:@//<your-db-host>:<port>/<service-name> (e.g., jdbc:oracle:thin:@//localhost:1521/ORCLPDB1 for a pluggable database)
    • User Name: Your Oracle database username (e.g., SYSTEM or a custom user you created)
    • Password: The password for that user
  • Click Test Connection to verify everything works. If the test passes, click Finish to save the connection.
Step 3: Java Code to Connect and Interact with the Database

Here's a complete Java code example that connects to the Oracle database, runs a simple query, and handles resources properly using try-with-resources (which automatically closes connections/streams):

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class OracleDBConnector {
    public static void main(String[] args) {
        // Replace these values with your own database details
        String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
        String username = "your_username";
        String password = "your_password";

        // Try-with-resources ensures all resources are closed automatically
        try (Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM your_table_name")) {

            System.out.println("Successfully connected to the database!");

            // Iterate through the result set and print data
            while (resultSet.next()) {
                // Replace "column_name" with actual column names from your table
                String data = resultSet.getString("column_name");
                System.out.println("Data: " + data);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Connection failed: " + e.getMessage());
        }
    }
}

Notes:

  • Make sure the ojdbc JAR is added to your project's build path (right-click project > Build Path > Add External Archives and select the downloaded JAR).
  • If you're using JDBC 4.0 or newer (which you are with ojdbc6/8), you don't need to explicitly load the driver with Class.forName("oracle.jdbc.driver.OracleDriver")—the DriverManager will find it automatically.
  • Always handle SQL exceptions properly in production code (don't just print the stack trace, add meaningful error handling).

内容的提问来源于stack exchange,提问作者DarkNight

火山引擎 最新活动