如何在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.
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
ojdbc8for Oracle 12c, 18c, 19c, or 21c - Use
ojdbc6for 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>
Let's configure the direct connection in Eclipse's Database Development perspective:
- Open Eclipse, go to
Window > Perspective > Open Perspective > Other > Database Developmentto switch to the database-focused view. - In the Data Source Explorer panel (usually on the left), right-click
Database Connectionsand selectNew. - 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, clickNew Driver Definition, thenAdd JAR/Zipto select the downloadedojdbcJAR 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:ORCLfor a local Oracle DB) - Service name format:
jdbc:oracle:thin:@//<your-db-host>:<port>/<service-name>(e.g.,jdbc:oracle:thin:@//localhost:1521/ORCLPDB1for a pluggable database)
- SID format:
- User Name: Your Oracle database username (e.g.,
SYSTEMor a custom user you created) - Password: The password for that user
- Database URL: Use either format:
- Click
Test Connectionto verify everything works. If the test passes, clickFinishto save the connection.
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
ojdbcJAR is added to your project's build path (right-click project >Build Path > Add External Archivesand 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




