【问题标题】:java.sql.SQLException: ORA-06502: PL/SQL: numeric or value error: character to number conversiojava.sql.SQLException:ORA-06502:PL/SQL:数字或值错误:字符到数字的转换
【发布时间】:2018-01-31 04:09:00
【问题描述】:

我正在尝试使用过程将记录插入到 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: at line 1\n"

虽然记录成功插入数据库,但语句执行后出现错误。

【问题讨论】:

    标签: java sql oracle procedure


    【解决方案1】:

    您应该使用 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;  
    / 
    

    【讨论】:

    • 我只是改变了java代码中outparams在程序中声明的顺序,解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多