我已经完成了以下示例JDBC程序检索用户详细信息。现在我感到惊讶的是,从相同的可调用语句我得到相同的输出参数索引的不同结果集。理想情况下它应该返回相同的ResultSet对象。
,
线程“main”中的异常java.sql.SQLException:缺少定义
System.out.println("Before loading connection");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@170.45.3.165:1541/testdb.mycomp.com",
"admin", "admin123");
System.out.println("Connection loaded " + connection);
CallableStatement callProcedure = connection
.prepareCall("{call ADMIN_USER.Fetch_User_Details(?,?)}");
callProcedure.setString(1, "userid=testid");
callProcedure.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
callProcedure.execute();
ResultSet resultUserDetails = (ResultSet) callProcedure.getObject(2);
resultUserDetails.next();
ResultSet resultUserDetails1 = (ResultSet) callProcedure.getObject(2);
String username = resultUserDetails1.getString(COL_NAME_USER_NAME);
System.out.println(resultUserDetails);
System.out.println(resultUserDetails1);
System.out.println(resultUserDetails == resultUserDetails1);
任何人都知道为什么我们从可调用语句中为相同的输出索引获取两个不同的结果集?
答案 0 :(得分:0)
根据CallableSatement API,表达式应该是这样的
{call <procedure-name>[(<arg1>,<arg2>, ...)]}
尝试
CallableStatement callProcedure = connection.prepareCall("{call ADMIN_USER.Fetch_User_Details(?,?)}");
答案 1 :(得分:0)
您是否尝试检查是否只有引用不同(可能是由于代理或脏状态,因为您在第二个`getObject()之前执行了next()
?
尝试
ResultSet resultUserDetails = (ResultSet) callProcedure.getObject(2);
ResultSet resultUserDetails1 = (ResultSet) callProcedure.getObject(2);
System.out.println(resultUserDetails);
System.out.println(resultUserDetails1);
// Check ref or object equality
System.out.println(resultUserDetails == resultUserDetails1);
System.out.println(resultUserDetails.equals(resultUserDetails1));
resultUserDetails.next();
String username = resultUserDetails.getString(COL_NAME_USER_NAME);
resultUserDetails1.next();
String username1 = resultUserDetails1.getString(COL_NAME_USER_NAME);
System.out.println(username);
System.out.println(username1);
// We read the same username or we are reading first and second username?
// If the are different probably resultset is the same, just with different
// reference
System.out.println(resultUserDetails.equals(resultUserDetails1));