JDBC 存储过程IN参数
2018-03-21 14:19 更新
JDBC教程 - JDBC 存储过程IN参数
以下示例显示如何使用in参数调用存储过程。
下面列出了用PL/SQL编写的Oracle存储过程。它向Person表插入一行。过程中的参数都标记为IN作为输入参数。
CREATE OR REPLACE PROCEDURE insertPERSON( p_userid IN PERSON.USER_ID%TYPE, p_username IN PERSON.USERNAME%TYPE, p_createdby IN PERSON.CREATED_BY%TYPE, p_date IN PERSON.CREATED_DATE%TYPE) IS BEGIN INSERT INTO PERSON ("USER_ID", "USERNAME", "CREATED_BY", "CREATED_DATE") VALUES (p_userid, p_username,p_createdby, p_date); COMMIT; END; /
例子
以下代码从 Connection
创建一个 CallableStatement
并传递存储过程的名称。
存储过程的参数都标有问号。
然后参数从 CallableStatement
设置为setXXX方法的值。
import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; public class Main { private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:YourDatabase"; private static final String DB_USER = "user"; private static final String DB_PASSWORD = "password"; public static void main(String[] argv) throws Exception { Class.forName(DB_DRIVER); Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); CallableStatement callableStatement = null; String insertStoreProc = "{call insertPERSON(?,?,?,?)}"; java.util.Date today = new java.util.Date(); callableStatement = dbConnection.prepareCall(insertStoreProc); callableStatement.setInt(1, 1000); callableStatement.setString(2, "name"); callableStatement.setString(3, "system"); callableStatement.setDate(4, new java.sql.Date(today.getTime())); callableStatement.executeUpdate(); System.out.println("Record is inserted into PERSON table!"); callableStatement.close(); dbConnection.close(); } }
以上内容是否对您有帮助:
更多建议: