[英]calling oracle PL/SQL function in java - Invalid column type error
我想从java类调用plsql函数,但是该函数(isValidPeriod)返回布尔数据类型,而JDBC不支持它
There is a simple workaround for that restriction, which does not require a wrapper function, simply wrap the boolean function itself in a CASE
statement and use an Integer for binding: 该限制有一个简单的解决方法,它不需要包装函数,只需将布尔函数本身包装在
CASE
语句中并使用Integer进行绑定即可:
stmt = conn.prepareCall(
"BEGIN"
+ " ? := CASE WHEN (myBooleanFuncInOracle()) "
+ " THEN 1 "
+ " ELSE 0"
+ " END;"
+ "END;");
stmt.registerOutParameter(1, Types.INTEGER);
// exec
stmt.execute();
// get boolean from Integer
boolean myBool = (stmt.getInt(1) == 1);
Very useful if you can't or don't want to change your function or even can't create a wrapper function, ie in legacy DBs. 如果您不能或不想更改函数,甚至不能创建包装函数,例如在旧版数据库中,则非常有用。
Due to a restriction in the OCI layer, the JDBC drivers do not support the passing of BOOLEAN parameters to PL/SQL stored procedures.
由于OCI层的限制,JDBC驱动程序不支持将BOOLEAN参数传递给PL / SQL存储过程。 The java boolean type does not match with the PL/SQl boolean type.
java布尔类型与PL / SQl布尔类型不匹配。 The Oracle documentation indicates that BOOLEAN is a PL/SQL type only, and there is no mapping between the "java.lang.Boolean" class and the PL/SQL BOOLEAN data type.
Oracle文档指出BOOLEAN仅是PL / SQL类型,在“ java.lang.Boolean”类和PL / SQL BOOLEAN数据类型之间没有映射。
So I am afraid you may have to change your database PL/SQL function to return an integer instead 因此,恐怕您可能必须更改数据库PL / SQL函数以返回整数
or 要么
just create a wrapper: function isValidPeriodInt(<params>) return integer is begin <call isValidPeriod and return 1 on true, 0 on false> end;
只需创建一个包装器即可:
function isValidPeriodInt(<params>) return integer is begin <call isValidPeriod and return 1 on true, 0 on false> end;
Starting in the 12.2 version of the JDBC-thin driver, there is native support for the PLSQL BOOLEAN type. 从JDBC瘦驱动程序的12.2版本开始,对PLSQL BOOLEAN类型提供了本机支持。
For IN parameters you would do: 对于IN参数,您可以执行以下操作:
CallableStatement cstmt = conn.prepareCall(sql);
// Make the driver send the Java "false" boolean as a PLSQL BOOLEAN type:
cstmt.setObject(2, false, OracleTypes.PLSQL_BOOLEAN);
...
cstmt.execute();
...
and for OUT parameters you do: 对于OUT参数,您可以执行以下操作:
CallableStatement cstmt = conn.prepareCall(sql);
// The driver should expect a PLSQL BOOLEAN type for the first OUT argument
// of the PLSQL procedure:
cstmt.registerOutParameter(1, OracleTypes.PLSQL_BOOLEAN);
cstmt.execute();
...
boolean likeTrump = cstmt.getBoolean(1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.