The best practice of OJDBC8 framework in the development of Java libraries
The best practice of OJDBC8 framework in the development of Java libraries
Overview:
OJDBC8 is an official provided by Oracle for the driver of the Java program connection and operation of the Oracle database.It provides some powerful functions and APIs that help developers to easily interact with the Oracle database.This article will introduce the best practice of using the OJDBC8 framework in the development of the Java library, including the techniques of configuration and connecting databases, performing database operations and abnormal processing.
1. Configuration and connection database
Before using the OJDBC8 framework, we need to ensure that the database connection information has been correctly configured.Generally speaking, the database connection information can be stored in the configuration file, such as the Properties file.Then, obtain the database connection information by reading the configuration file, and use the following code fragment to establish a database connection:
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DatabaseUtils {
public static Connection getConnection() throws SQLException, IOException {
try (InputStream input = DatabaseUtils.class.getClassLoader().getResourceAsStream("config.properties")) {
Properties properties = new Properties();
properties.load(input);
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
return DriverManager.getConnection(url, username, password);
}
}
}
The above code obtains the database connection information by loading the configuration file, and uses the `DriverManager.GetConnection () method to establish a database connection.In this way, we decide the database connection information from the code to facilitate subsequent maintenance and management.
2. Execute the database operation
Once a database connection is established, we can use the OJDBC8 API to perform various database operations, including query, inserting, updating, and deleting.Here are some common operation examples:
- Query data:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseUtils {
public static void queryData() throws SQLException {
try (Connection connection = DatabaseUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM users")) {
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
// Process query results
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("Name: " + name + ", Age: " + age);
}
}
}
}
}
-Stch -in data:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseUtils {
public static void insertData(String name, int age) throws SQLException {
try (Connection connection = DatabaseUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO users (name, age) VALUES (?, ?)")) {
preparedStatement.setString(1, name);
preparedStatement.setInt(2, age);
preparedStatement.executeUpdate();
}
}
}
- update data:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseUtils {
public static void updateData(String name, int age, int id) throws SQLException {
try (Connection connection = DatabaseUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE users SET name = ?, age = ? WHERE id = ?")) {
preparedStatement.setString(1, name);
preparedStatement.setInt(2, age);
preparedStatement.setInt(3, id);
preparedStatement.executeUpdate();
}
}
}
- delete data:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseUtils {
public static void deleteData(int id) throws SQLException {
try (Connection connection = DatabaseUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM users WHERE id = ?")) {
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
}
}
}
In the above examples, we use the `PreparedStatement` object to pre -compiled the SQL statement to improve execution efficiency and security.By setting the parameters and calling `Executequry ()` or `ExecuteupDate (), we can perform query and update operations.
3. Dreatment
When using the OJDBC8 framework, we also need to deal with possible abnormalities.Here are some commonly used abnormal processing skills:
-Check whether the connection is successfully established:
import java.sql.Connection;
import java.sql.SQLException;
public class DatabaseUtils {
public static Connection getConnection() throws SQLException {
// ...
Connection connection = DriverManager.getConnection(url, username, password);
if (connection != null) {
System.out.println ("Connected to be successfully established!");
} else {
System.out.println ("Connection fails!");
}
// ...
}
}
-Add the connection and resource with Try-With-Resources statement:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseUtils {
public static void insertData(String name, int age) throws SQLException {
try (Connection connection = DatabaseUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO users (name, age) VALUES (?, ?)")) {
preparedStatement.setString(1, name);
preparedStatement.setInt(2, age);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// Treatment abnormalities
e.printStackTrace();
}
}
}
Reasonable treatment of abnormalities can improve the robustness and reliability of procedures, and avoid problems such as procedure crash and data loss caused by unprocessed abnormalities.
in conclusion:
The OJDBC8 framework is the best choice to connect and operate Oracle database in the development of the Java library.Through correct configuration and connection database, and using a suitable API to perform database operations, developers can easily interact with the Oracle database.Coupled with a good abnormal processing mechanism, it can ensure the stability and reliability of the program.The best practice introduced above will help developers better use the OJDBC8 framework for Oracle database development.