The application example of the JDBC 2.0 Optional Package framework in the database operation

The application example of the JDBC 2.0 Optional Package framework in the database operation JDBC (Java DataBase Connectivity) is a standard API used in Java programming to connect and operate with various databases.JDBC 2.0 Optional Package (also known as the JDBC 2.0 expansion) is part of the JDBC 2.0 specification, which provides additional functions and extensions for JDBC. Here are an application examples of database operation using the JDBC 2.0 Optional Package framework.In this example, we will use Java to write a simple database application for connecting to the database to perform query and operation data. First, we need to prepare databases.Suppose we have a MySQL database called "MyDataBase", and a table called "Users" is created in it, including "ID" and "name". import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcExample { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { // Connect to the database connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyDatabase", "username", "password"); // Create a statement object statement = connection.createStatement(); // Execute the query resultSet = statement.executeQuery("SELECT * FROM users"); // Traversing results set and output data while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); System.out.println("ID: " + id + ", Name: " + name); } // Execute the update operation int rowsAffected = statement.executeUpdate("INSERT INTO users (id, name) VALUES (1, 'John')"); System.out.println(rowsAffected + " rows affected."); } catch (SQLException e) { e.printStackTrace(); } finally { try { // Release resources if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } In the above code, we first use DriverManager to obtain the database connection, connecting "LocalHost: 3306" in the connection string represents the address and port number of the database server, "MyDataBase" means the names of the database to be connected, "username" and "password"They are the username and password of the database. We then create the Statement object to execute the SQL statement.In this example, we perform a query operation in the "USERS" table to output the data of the result set to the console. Next, we execute a update operation and insert a new record to the "USERS" table.In this example, we inserted a record with an ID of 1, named "John". Finally, we use Try-Catch-Finally blocks to deal with abnormalities and release the resources used in Finally blocks, including ResultSet, Statement, and Connections. Through the JDBC 2.0 Optional Package framework, we can easily connect to the database to perform query and update operations.Such applications can be applied to various types of databases, including MySQL, Oracle, SQL Server, etc. Please note that the database connection information, query statements and table structures in the example are for reference only, and you need to adjust according to your actual situation.