java.sql.SQLException:ORA-06502:PL / SQL:数字或值错误:字符到数字转换

时间:2017-08-22 18:53:47

标签: java sql oracle procedure

我正在尝试使用过程将记录插入到oracle数据库中。插入记录但发生异常。

public class Myproc  extends StoredProcedure {


public ThresholdOperationsSP(JdbcTemplate jdbcTemplate, String sql) {
        super(jdbcTemplate, sql);
        // Declare IN params
        declareParameter(new SqlParameter("i_unique_col", Types.VARCHAR));
        declareParameter(new SqlParameter("i_action", Types.VARCHAR));
        declareParameter(new SqlParameter("i_table", Types.VARCHAR));
        declareParameter(new SqlParameter("i_columns", Types.VARCHAR));
        declareParameter(new SqlParameter("i_values", Types.VARCHAR));
        declareParameter(new SqlParameter("i_where_col", Types.VARCHAR));
        declareParameter(new SqlParameter("i_where_values", Types.VARCHAR));
        declareParameter(new SqlParameter("i_connection", Types.VARCHAR));
        // Declare OUT params
        declareParameter(new SqlOutParameter("o_unique_id", Types.INTEGER));
        declareParameter(new SqlOutParameter("o_proc_msg", Types.VARCHAR));
        declareParameter(new SqlOutParameter("o_proc_code", Types.INTEGER));
        declareParameter(new SqlOutParameter("o_sql", Types.VARCHAR));


    compile();
}

@SuppressWarnings("unchecked")
public Map<String, Object> execute(String connectionName, String tableName, String action,
        String unqiueIdColumn, String colNames, String newValues, String whereColNames, String whereColValues)  {
    // Process input params & assign to map
    Map<String, Object> inParams = new HashMap<String, Object>();

    inParams.put("i_unique_col", unqiueIdColumn);
    inParams.put("i_action", action);
    inParams.put("i_table", tableName);
    inParams.put("i_columns", colNames);
    inParams.put("i_values", newValues);
    inParams.put("i_where_col", whereColNames);
    inParams.put("i_where_values", whereColValues);
    inParams.put("i_connection", connectionName);

    // Execute SP
    Map<String, Object> resultSummary = execute(inParams);
    System.out.println(resultSummary);
    return resultSummary;
}

将上述代码称为:

Map<String, Object> resultsSummary = new Myproc(jdbcTemplate,"MYPROCEDURE").execute("CON1","table1","INSERT","COL1","col2","33",null,null);

以上代码调用以下过程;

     create or replace procedure          MYPROCEDURE
(
    i_unique_col      in          varchar2,
    i_action          in          varchar2,
    i_table           in          varchar2,
    i_columns         in          varchar2,
    i_values          in          varchar2,
    i_where_col       in          varchar2,
    i_where_values    in          varchar2,
    i_connection      in          varchar2,
    o_unique_id       out         number,
    o_sql             out         varchar2,
    o_proc_msg        out         varchar2,
    o_proc_code       out         number 
)
as

    p_c_proc_name constant varchar2(50):= 'MYPROCEDURE';  

    p_v_err_cd              number;
    p_v_err_msg             varchar2(500);
    p_v_result              varchar2(1);
    p_v_timestam            timestamp with time zone;
    p_v_values              varchar2(32767);
    p_v_sql                 varchar2(32767);
    p_v_where               varchar2(32767);

begin

    dbms_application_info.set_module (module_name=> p_c_proc_name, action_name=> 'PROCEDURE INSERT-EDIT ACTIONS');

    if upper(i_connection) = 'CON1' then

        p_v_values := ''''||replace(i_values,',',''',''')||'''';

        if upper(i_action) = 'INSERT' then

            case when i_unique_col is not null then
                p_v_sql := 'insert into uno_comp.'||i_table||' ('||i_columns||' ) values ('||p_v_values||') returning '||i_unique_col||' into :1' ;
                execute immediate p_v_sql using out o_unique_id ;
            else
                p_v_sql :='insert into uno_comp.'||i_table||' ('||i_columns||' ) values ('||p_v_values||')';
                execute immediate p_v_sql;
            end case;                                                     
            commit;
        end if;            
        p_v_result := uno_comp.ulog('INFO', p_c_proc_name, 'Successfully performed  insert-edit actions '|| p_v_sql, null, null, null );
        o_proc_msg  := 'procedure Edit Action Complete.';
        o_proc_code := 0;
   end if; 
end MYPROCEDURE;

执行语句时发生错误

Map<String, Object> resultSummary = execute(inParams);
  

org.springframework.jdbc.BadSqlGrammarException:   CallableStatementCallback;错误的SQL语法[{call MYPROCEDURE(?,?,?,   ?,?,?,?,?,?,?,?,?)}];嵌套异常是   java.sql.SQLException:ORA-06502:PL / SQL:数值或值错误:   字符到数字转换错误\ nORA-06512:在第1行\ n“

虽然记录已成功插入数据库,但在执行语句后会发生错误。

1 个答案:

答案 0 :(得分:0)

您应该使用JDBC CallableStatements来调用过程。 How to Use Callable Statement

一个工作实例如下---

import java.sql.*;  
public class Proc 
{  
    public static void main(String[] args) throws Exception
    {     
        Class.forName("oracle.jdbc.driver.OracleDriver");  
        Connection con=DriverManager.getConnection(  
        "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  

        CallableStatement stmt=con.prepareCall("{call insertR(?,?)}");  
        stmt.setInt(1,1011);  
        stmt.setString(2,"Amit");  
        stmt.execute();  

        System.out.println("success");  
    }  
}

程序的结构如下---

create or replace procedure "INSERTR"  
(id IN NUMBER,  
name IN VARCHAR2)  
is  
begin  
insert into user420 values(id,name);  
end;  
/