How to resolve: CallableStatement executing returns error “ORA-06502: PL/SQL: numeric or value error: character to number conversion error”?

两盒软妹~` 提交于 2019-12-11 14:20:03

问题


I try to put data from string filed of one table to number field of another table.

If I do this in some IDE like Oracle SQL Developer or Toad it works fine, but if I do this in my java code I got error.

How to resolve it?

There is my example below.

database: I create two tables - t_num with number field and t_str with varchar2 field - and put values to second table t_str

drop table t_num;
create table t_num(num number(10,2));
drop table t_str;
create table t_str(str varchar2(10));
insert into t_str (str) values('23');
insert into t_str (str) values('2 3');
insert into t_str (str) values('2 3,3 2');
commit;

also I create procedure, that put values from t_str to t_num

create or replace procedure put_to_t_num
as
    type t_num_t is table of t_num%rowtype index by binary_integer;
    tn t_num_t;
    n binary_integer := 0;
begin
    delete from t_num;
    --tn := t_num_t();
    for rec in ( select * from t_str )
    loop
        n := n + 1;
        --tn.extend;
        tn(n).num := to_number( regexp_replace( regexp_replace( rec.str, ',', '.'), ' ', '' ) );
    end loop;

    forall i in 1..n
        insert into t_num (
            num
        ) values (
            tn(i).num
        );
        --commit;
end;

weblogic server:

index.jsp: click on the button send a request to put.jsp

<button>click</button>
<script>
document.querySelector("button").addEventListener("click", function () {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "put.jsp");
    xhr.send(null);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == xhr.DONE && xhr.status == 200) {
            console.log(xhr.responseText);
        }
    }
});
</script>

put.jsp: gets connection from datasource with jndi name jdbc/test and calls procedure put_to_t_num

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page import="javax.annotation.Resource"%>
<%!
@Resource(name="jdbc/test")
private DataSource ds = null;
%>
<%@ page import="javax.sql.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="java.util.*"%>

<%
Context ctx = null;
try {
    ctx = new InitialContext();
    ds = (DataSource) ctx.lookup("jdbc/test");
} catch (Exception e) {
    out.print(e.getMessage());
}
Connection con = null;
CallableStatement cstmt = null;
Statement stmt = null;
try {
    //call procedure
    con = ds.getConnection();
    cstmt = con.prepareCall("{call put_to_t_num}");
    cstmt.execute();
    //commit
    stmt = con.createStatement();
    stmt.executeUpdate("commit");
    out.print("success");
} catch (SQLException e) {
    out.print("catch error:\n" + e.getMessage());
} finally {
    try {
        if (cstmt != null) {
            cstmt.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (con != null) {
            con.close();
        }
    } catch (Exception e) {
        out.print("finally error:\n" + e.getMessage());
    }
}
%>

In the end I got error in browser console:

catch error:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "TEST_USER.PUT_TO_T_NUM", line 13
ORA-06512: at line 1

But it work fine if I do something like this in IDE

begin
    put_to_t_num;
    commit;
end;

I guess that could be the case in encodings, but I can't resolve it in my java code.

select * from nls_database_parameters;

NLS_LANGUAGE    AMERICAN
NLS_TERRITORY   AMERICA
NLS_CURRENCY    $
NLS_ISO_CURRENCY    AMERICA
NLS_NUMERIC_CHARACTERS  .,
NLS_CHARACTERSET    AL32UTF8
NLS_CALENDAR    GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE   AMERICAN
NLS_SORT    BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT    DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT  HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY   $
NLS_COMP    BINARY
NLS_LENGTH_SEMANTICS    BYTE
NLS_NCHAR_CONV_EXCP FALSE
NLS_NCHAR_CHARACTERSET  AL16UTF16
NLS_RDBMS_VERSION   11.2.0.2.0

How to resolve this problem?

Also I discover that if no adding value 2 3,3 2 to the t_str - I got "success".

How can I process this situation?

Why IDE do it, but java code do not do it?

来源:https://stackoverflow.com/questions/29286665/how-to-resolve-callablestatement-executing-returns-error-ora-06502-pl-sql-nu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!