gpt4 book ai didi

java - JDBC 如何使用 CallableStatement 创建过程?

转载 作者:行者123 更新时间:2023-11-29 04:53:37 25 4
gpt4 key购买 nike

有人知道如何使用 CallableStatement 创建带有变量的过程吗?

Connection targetDBconn = null;
CallableStatement cs = null;


try {
cs = targetDBconn.prepareCall(SqlList.createProcedure_PROC_BACKUP_DATABASE);
cs.setString(1, targetRmanBackupSettingsInfo.getFile_db_backup());
cs.execute();
}
catch (SQLException ex


) {
return null;
}


finally {if (cs != null) {
try {
cs.close();
} catch (SQLException ex) {
}
}
if (targetDBconn != null) {
try {
targetDBconn.close();
} catch (SQLException ex) {
}
}
}

像这样的 SqlList.createProcedure_PROC_BACKUP_DATABASE

public static String createProcedure_PROC_BACKUP_DATABASE
= "CREATE OR REPLACE PROCEDURE SYS.PROC_BACKUP_DATABASE\n"
+ "IS \n"
+ " V_TMP VARCHAR2(100);\n"
+ " V_BKP_STATUS VARCHAR2(100); \n"
+ "BEGIN \n"
+ "\n"
+ " SELECT STATUS INTO V_BKP_STATUS \n"
+ " FROM ( SELECT STATUS\n"
+ "FROM V$RMAN_BACKUP_JOB_DETAILS\n"
+ "WHERE INPUT_TYPE = 'DB FULL' \n"
+ "ORDER BY SESSION_KEY DESC) \n"
+ " WHERE ROWNUM = 1;\n"
+ "\n"
+ " IF (V_BKP_STATUS = 'RUNNING') THEN \n"
+ " RETURN;\n"
+ " END IF; \n"
+ " SELECT OS_COMMAND.EXEC(?) INTO V_TMP FROM DUAL; \n"
// + " --SELECT OS_COMMAND.EXEC(?) INTO V_TMP FROM DUAL;\n"
+ "\n"
+ "EXCEPTION\n"
+ "WHEN NO_DATA_FOUND\n"
+ "THEN \n"
+ "NULL; \n"
+ "WHEN OTHERS \n"
+ "THEN \n"
+ "-- Consider logging the error and then re-raise \n"
+ "RAISE;\n"
+ "END PROC_BACKUP_DATABASE;\n"
+ "/";

查询中有一个问号,我只想向其中插入一些字符串变量。但是我执行这段代码,发生ora-01003错误..

最佳答案

您使用 CallableStatement调用 存储过程。您不会用它来创建一个。常规的 Statement 是正确的用法。

当然,从 Java 代码创建过程听起来有点可疑。

关于java - JDBC 如何使用 CallableStatement 创建过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com