Using Java to Operate Oracle

To use Java to operate the Oracle Database, first install and configure the Oracle Database, and set the database connection information, including the database URL, user name, password, etc. Next, you can use the JDBC (Java Database Connectivity) API provided by Java to connect and operate the Oracle Database. The following are the basic steps for operating the Oracle Database: 1. Introducing Maven dependencies: Add Oracle JDBC driven Maven dependencies to the pom.xml file of the project: <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.11.0.0</version> </dependency> Please ensure that the Oracle JDBC driver used matches the version of the Oracle Database. 2. Create a database connection: Use Java code to create a database connection object. import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseConnection { private static final String URL = "jdbc:oracle:thin:@localhost:1521:XE"; private static final String USERNAME = "yourusername"; private static final String PASSWORD = "yourpassword"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection(URL, USERNAME, PASSWORD); } } Please replace the URL, USERNAME, and PASSWORD for your own database connection information. 3. Insert data: write Java code to insert data into Oracle Database. import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class InsertData { public static void main(String[] args) { String sql = "INSERT INTO employees (id, name) VALUES (?, ?)"; try (Connection connection = DatabaseConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, 1); statement.setString(2, "John"); int rowsInserted = statement.executeUpdate(); if (rowsInserted > 0) { System.out.println("A new row has been inserted."); } } catch (SQLException e) { e.printStackTrace(); } } } 4. Modify data: write Java code to modify data in Oracle Database. import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class UpdateData { public static void main(String[] args) { String sql = "UPDATE employees SET name = ? WHERE id = ?"; try (Connection connection = DatabaseConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setString(1, "Peter"); statement.setInt(2, 1); int rowsUpdated = statement.executeUpdate(); if (rowsUpdated > 0) { System.out.println("The row has been updated."); } } catch (SQLException e) { e.printStackTrace(); } } } 5. Query data: write Java code to query data from Oracle Database. import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class QueryData { public static void main(String[] args) { String sql = "SELECT * FROM employees"; try (Connection connection = DatabaseConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql); ResultSet resultSet = statement.executeQuery()) { while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); System.out.println("ID: " + id + ", Name: " + name); } } catch (SQLException e) { e.printStackTrace(); } } } 6. Delete data: write Java code to delete data in Oracle Database. import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class DeleteData { public static void main(String[] args) { String sql = "DELETE FROM employees WHERE id = ?"; try (Connection connection = DatabaseConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, 1); int rowsDeleted = statement.executeUpdate(); if (rowsDeleted > 0) { System.out.println("The row has been deleted."); } } catch (SQLException e) { e.printStackTrace(); } } } The above code example demonstrates how to use Java to operate Oracle Database, including inserting data, modifying data, querying data, and deleting data. According to specific requirements and table structure, corresponding modifications and extensions can be made.