java在使用registerOutParameter函数时将oracle的“OracleTypes.CURSOR”替换为mysql中的等效项
亲爱的stackoverflow社区
我正在尝试将一个用JAVA开发的系统从使用oracle DB升级到mysql DB
我已经到了使用存储过程的地步,java代码使用了特定的oracle结构-OracleTypes.CURSOR
。请参阅下面的代码
private int getUserID(String username) {
int UserID = -1;
CallableStatement cs = null;
ResultSet rs = null;
try {
con = this.getConnection();
cs = con.prepareCall("{call getUserID(?,?)}");
cs.setString(1, username);
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
rs = (ResultSet) cs.getObject(2);
if (rs.next()) {
UserID = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
cs.close();
rs.close();
} catch (SQLException ex) {
Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
return UserID;
}
}
我想做的是将OracleTypes.CURSOR
的用法转换成mysql友好的用法。
我确实读到,在mysql的较新版本中可以这样做,所以我甚至尝试用java.sql.Types.INTEGER
替换OracleTypes.CURSOR
,但没有任何结果
你能帮我吗
注意:我在这里有以下关于堆栈溢出和其他地方的信息,如: http://bugs.mysql.com/bug.php?id=17898
# 1 楼答案
不幸的是,MySql没有实现
ref cursors
但是,MySql中的存储过程可以将结果集返回给客户端,请参见以下链接:http://dev.mysql.com/doc/refman/5.6/en/faqs-stored-procs.html#qandaitem-B-4-1-14
只需运行一个普通SELECT,结果集就会返回给客户端
结果集的概念与Oracle中的
ref cursor
非常相似。最显著的区别是,在MySql中,结果集完全从服务器检索到客户端,并存储在客户端的内存中(而Oraclecursors
以块的形式检索行)但是,MySql可以模拟这种行为(有一些限制),请参见以下链接:http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
它是如何工作的——一个例子
假设我们有一个要迁移到MySql的oracle过程:
在MySql中,此过程可能如下所示:
和Java代码-基于文档中的示例:http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-statements-callable.html#connector-j-examples-retrieving-results-params