内容或简介:
/**
调用数据库里的一个函数
一个函数本质上一个返回一个结果的存储过程,这个例子示范了怎么调用有in、out和in/out参数的函数
***********************************/
callablestatement cs;
try {
// 调用一个没有参数的函数; 函数返回 a varchar
// 预处理callable语句
cs = connection.preparecall("{? = call myfunc}");
// 注册返回值类型
cs.registeroutparameter(1, i);
// execute and retrieve the returned value
cs.execute();
string retvalue = cs.getstring(1);
// 调用有一个in参数的函数; the function returns a varchar
cs = connection.preparecall("{? = call myfuncin(?)}");
// register the type of the return value
cs.registeroutparameter(1, types.varchar);
// set the value for the in parameter
cs.setstring(2, "a string");
// execute and retrieve the returned value
cs.execute();
retvalue = cs.getstring(1);
// 调用有一个out参数的函数; the function returns a varchar
cs = connection.preparecall("{? = call myfuncout(?)}");
// register the types of the return value and out parameter
cs.registeroutparameter(1, types.varchar);
cs.registeroutparameter(2, types.varchar);
// execute and retrieve the returned values
cs.execute();
retvalue = cs.getstring(1); // return value
string outparam = cs.getstring(2); // out parameter
// 调用有一个in/out参数的函数; the function returns a varchar
cs = connection.preparecall("{? = call myfuncinout(?)}");
// register the types of the return value and out parameter
cs.registeroutparameter(1, types.varchar);
cs.registeroutparameter(2, types.varchar);
// set the value for the in/out parameter
cs.setstring(2, "a string");
// execute and retrieve the returned values
cs.execute();
retvalue = cs.getstring(1); // return value
outparam = cs.getstring(2); // in/out parameter
} catch (sqlexception e) {
}
闽公网安备 35060202000074号