java连接oracle数据库代码

To connect Java with Oracle database, you can use JDBC (Java Database Connectivity). Below is a simple example of Java code to connect to an Oracle database:

java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class OracleDatabaseConnection { public static void main(String[] args) { // JDBC URL, username, and password of Oracle database String jdbcUrl = "jdbc:oracle:thin:@//your-oracle-host:1521/your-service-name"; String username = "your-username"; String password = "your-password"; // Load Oracle JDBC Driver try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.err.println("Error loading Oracle JDBC driver. Make sure to include the JDBC driver in your project."); e.printStackTrace(); return; } // Establish a connection try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) { System.out.println("Connected to Oracle Database!"); // Your database operations go here } catch (SQLException e) { System.err.println("Error connecting to Oracle Database."); e.printStackTrace(); } } }

Make sure to replace the placeholders such as your-oracle-host, your-service-name, your-username, and your-password with the actual values for your Oracle database.

Additionally, you need to have the Oracle JDBC driver JAR file included in your project. You can download the driver from the official Oracle website (https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html) and add it to your project's classpath.

Note: The above code uses the try-with-resources statement, which automatically closes the Connection when it's no longer needed. Also, handle exceptions appropriately based on your application requirements.

Certainly! Once you have established a connection to the Oracle database, you can perform various operations such as executing SQL queries, updating data, and closing the connection. Here's an extension of the previous example that includes executing a simple SQL query:

java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class OracleDatabaseConnection { public static void main(String[] args) { // JDBC URL, username, and password of Oracle database String jdbcUrl = "jdbc:oracle:thin:@//your-oracle-host:1521/your-service-name"; String username = "your-username"; String password = "your-password"; // Load Oracle JDBC Driver try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.err.println("Error loading Oracle JDBC driver. Make sure to include the JDBC driver in your project."); e.printStackTrace(); return; } // Establish a connection try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) { System.out.println("Connected to Oracle Database!"); // Example: Execute a simple SQL query String sqlQuery = "SELECT * FROM your_table_name"; try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery); ResultSet resultSet = preparedStatement.executeQuery()) { // Process the result set while (resultSet.next()) { // Retrieve data from the result set int column1Value = resultSet.getInt("column1"); String column2Value = resultSet.getString("column2"); // Process the data as needed System.out.println("Column1: " + column1Value + ", Column2: " + column2Value); } } catch (SQLException e) { System.err.println("Error executing SQL query."); e.printStackTrace(); } } catch (SQLException e) { System.err.println("Error connecting to Oracle Database."); e.printStackTrace(); } } }

Make sure to replace your_table_name, column1, and column2 with the actual table name and column names from your database.

This example shows how to execute a simple SELECT query and process the results. Depending on your application requirements, you can modify the SQL queries and perform other operations like inserts, updates, and deletes. Always remember to handle exceptions appropriately in a production environment.

标签