0


我已经完成了下面的示例 JDBC 程序,该程序检索用户详细信息。现在我很惊讶从相同的可调用语句中我得到相同输出参数索引的不同结果集。理想情况下,它应该返回相同的 ResultSet 对象。

  1. 当我得到结果集时,我将光标 -1 移动到 0。
  2. 我正在使用列名从另一个结果集中检索相同输出参数的数据然后我得到以下异常

,

线程“主”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);

谁能知道为什么我们从可调用语句中获得相同输出索引的两个不同结果集?

4

2 回答 2

0

您是否尝试检查是否只有引用不同(可能是由于代理或脏状态,因为您next()在第二个`getObject()之前执行了a)?
尝试

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));
于 2013-08-07T11:00:20.600 回答
0

根据 CallableStatement API,表达式应该是这样的

 {call <procedure-name>[(<arg1>,<arg2>, ...)]}

尝试

CallableStatement callProcedure = connection.prepareCall("{call ADMIN_USER.Fetch_User_Details(?,?)}");
于 2013-08-07T09:51:59.163 回答