Oracle stored procedures can be called through Java programs, which is a way to access the database using Java technology. This article explains how to combine stored procedures with Java and provides relevant sample code.
1. Use JDBC to access Oracle database
Java Database Connection (JDBC) is a standard API for connecting to various databases, including Oracle database. Before using JDBC to access the Oracle database, you need to perform the following steps:
1. Download and install the Oracle database.
2. Download and install Java Development Kit (JDK).
3. Download and install the Oracle JDBC driver.
There are two ways to download the Oracle JDBC driver:
-Oracle official website download: https://www.oracle.com/database/technologies/jdbc-drivers-12c-downloads. html
-Maven dependency:
<dependency> <groupId>com.oracle.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>12.2.0.1</version> </dependency>
4. Configure database connection information.
Database connection information usually includes:
-The host name or IP address of the database.
-Port of the database.
- The name of the database.
- Username and password for the database.
You can configure database connection information in the following ways:
String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String user = "username"; String password = "password";
5. Connect to the database.
Use the following code to connect to the database:
Connection connection = DriverManager.getConnection(url, user, password);
Among them, the DriverManager.getConnection
method accepts three parameters: URL, username and password.
After connecting to the database, you can use Java programs to perform various operations such as query, insert, update, and delete.
2. Introduction to Oracle stored procedures
A stored procedure is a pre-compiled program that can execute a series of SQL statements to complete a specific task. Stored procedures have the following advantages:
-Improved performance: Stored procedures are pre-compiled, which reduces execution time and improves performance.
-Improved security: stored procedures ensure that permissions are limited to authorized users.
-Simplify programming: Stored procedures can encapsulate some common business logic and reduce code duplication.
3. Java calls Oracle stored procedures
The process of calling stored procedures in Java is divided into the following steps:
1. Create a CallableStatement.
Use the following code to create a CallableStatement:
CallableStatement cstmt = conn.prepareCall("{?= call procedure_name(?, ?, ...)}");
Among them, conn
is the database connection object, and procedure_name
is the stored procedure name.
?=
represents the return value, ?
represents the input parameter.
2. Set values for input parameters.
Use the following code to set the value for the input parameter:
cstmt.setString(2, "input_param");
Where, 2
represents the value that should be set to the second parameter, "input_param"
is the actual input parameter value.
3. Register output parameters.
If the stored procedure includes return values or output parameters, it needs to be registered through the registerOutParameter
method. For example:
cstmt.registerOutParameter(1, Types.INTEGER);
Among them, 1
indicates that the first parameter is the return value, and Types.INTEGER
indicates that the return value type is an integer.
4. Execute the stored procedure.
The code to execute the stored procedure is as follows:
cstmt.execute();
5. Get the return value or output parameters.
If the stored procedure includes return values or output parameters, you can use the following code to obtain the results:
int result = cstmt.getInt(1);
Where, 1
means that the first parameter is the return value.
To sum up, the sample code for calling Oracle stored procedures from Java is as follows:
try { Class.forName("oracle.jdbc.driver.OracleDriver"); // 加载 JDBC 驱动 Connection conn = DriverManager.getConnection(url, user, password); // 连接到数据库 CallableStatement cstmt = conn.prepareCall("{?= call procedure_name(?, ?)}"); // 创建 CallableStatement cstmt.registerOutParameter(1, Types.INTEGER); // 注册输出参数 cstmt.setString(2, "input_param1"); // 设置输入参数 cstmt.setString(3, "input_param2"); cstmt.execute(); // 执行存储过程 int result = cstmt.getInt(1); // 获取结果 conn.close(); // 关闭数据库连接 } catch (Exception e) { e.printStackTrace(); }
The above code demonstrates how to connect to the Oracle database, create a CallableStatement and execute the stored procedure.
Conclusion
This article introduces how to use Java programs to access Oracle database and call stored procedures. Using stored procedures simplifies programming and improves performance and security. By combining Java technology with stored procedures, you can access and operate Oracle databases more efficiently.
The above is the detailed content of oracle stored procedure calls java. For more information, please follow other related articles on the PHP Chinese website!