JDBC如何使用CallableStatement创建过程?

时间:2015-12-30 14:52:11

标签: java jdbc

有人知道如何使用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"
            + "/";

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

1 个答案:

答案 0 :(得分:3)

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

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