JDBC CallableStatement – ストアドプロシージャINパラメータの例
コードスニペットを使用して、JDBCの `CallableStatement`を介してOracleストアドプロシージャを呼び出す方法、およびJavaからストアドプロシージャにINパラメータを渡す方法を説明します。
….//insertDBUSER is stored procedure
String insertStoreProc = “{call insertDBUSER(?,?,?,?)}”;
callableStatement = dbConnection.prepareCall(insertStoreProc);
callableStatement.setInt(1, 1000);
callableStatement.setString(2, “mkyong”);
callableStatement.setString(3, “system”);
callableStatement.setDate(4, getCurrentDate());
callableStatement.executeUpdate();
=== JDBC CallableStatementの例 完全なJDBCの `CallableStatement`の例を参照してください。 === 1.ストアドプロシージャ Oracleデータベース内のストアド・プロシージャ。その後、JDBC経由で呼び出します。
CREATE OR REPLACE PROCEDURE insertDBUSER(
p
userid IN DBUSER.USER
ID%TYPE,
p
username IN DBUSER.USERNAME%TYPE,
p
createdby IN DBUSER.CREATED
BY%TYPE,
p
date IN DBUSER.CREATED__DATE%TYPE)
IS
BEGIN
INSERT INTO DBUSER ("USER__ID", "USERNAME", "CREATED__BY", "CREATED__DATE")
VALUES (p__userid, p__username,p__createdby, p__date);
COMMIT;
END;/….
2. CallableStatement経由でストアドプロシージャを呼び出します.
JDBCの例では、 `CallableStatement`を介してストアドプロシージャを呼び出します。
File:JDBC CallableStatement INパラメータExample.java
package com.mkyong.jdbc;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class JDBCCallableStatementINParameterExample {
private static final String DB__DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB__CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
private static final String DB__USER = "user";
private static final String DB__PASSWORD = "password";
public static void main(String[]argv) {
try {
callOracleStoredProcINParameter();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void callOracleStoredProcINParameter() throws SQLException {
Connection dbConnection = null;
CallableStatement callableStatement = null;
String insertStoreProc = "{call insertDBUSER(?,?,?,?)}";
try {
dbConnection = getDBConnection();
callableStatement = dbConnection.prepareCall(insertStoreProc);
callableStatement.setInt(1, 1000);
callableStatement.setString(2, "mkyong");
callableStatement.setString(3, "system");
callableStatement.setDate(4, getCurrentDate());
//execute insertDBUSER store procedure
callableStatement.executeUpdate();
System.out.println("Record is inserted into DBUSER table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (callableStatement != null) {
callableStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB__DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB__CONNECTION, DB__USER,DB__PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
private static java.sql.Date getCurrentDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}
}
完了しました。上記のJDBCの例を実行すると、新しいレコードがストアドプロシージャ “` insertDBUSER` “を介してデータベースに挿入されます。