【发布时间】:2015-01-22 13:11:02
【问题描述】:
我的问题与 this post 和 a post of mine 有关。我试图在使用 JDBC 调用 PL/SQL 过程时将 REF_CURSOR 作为 IN 参数传递。这是我的代码:
public int printMaxSalAllDept()
{
Connection conn = null;
OracleCallableStatement callStmt = null;
int rowCount = -1;
try
{
// Register the Jdbc Driver
// Class.forName(JDBC_DRIVER_ORACLE);
// Create a Database connection
conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
// Create a query string to get the ResultSet of your choice
String getRsQuery = "SELECT e.department_id , e.last_name , "
+ "e.salary FROM employees e , (SELECT department_id , "
+ "MAX(salary) AS maxSal FROM employees GROUP BY department_id) "
+ "m WHERE e.department_id = m.department_id "
+ "AND e.salary = m.maxSal ORDER BY e.salary";
// Create a Statement
Statement stmt = conn.createStatement();
// Execute the statement
ResultSet rs = stmt.executeQuery(getRsQuery);
// Create a SQL String
String callProc = "{ call HR.EMP_PKG.print_max_sal_all_dept(? , ?) }";
// Create a Callable Statement
callStmt = (OracleCallableStatement) conn.prepareCall(callProc);
// Bind values to the IN parameter
callStmt.setCursor(1, rs);
// callStmt.setNull(1,OracleTypes.CURSOR);
// Register OUT parameters type to the SQL type of the value returned
callStmt.registerOutParameter(2, java.sql.Types.NUMERIC);
// Execute Callable Statements
callStmt.execute();
// Retrieve value from the OUT parameters
rowCount = callStmt.getInt(0);
System.out.println("Number of rows in the cursor :" + rowCount);
}
catch (SQLException se)
{
System.out.println("Exception occured in the database");
System.out.println("Exception message: "+ se.getMessage());
System.out.println("Database error code: "+ se.getErrorCode());
se.printStackTrace();
}
finally
{
// Clean up
if(callStmt != null)
{
try
{
callStmt.close();
}
catch (SQLException se2)
{
se2.printStackTrace();
}
}
if(conn != null)
{
try
{
conn.close();
}
catch (SQLException se2)
{
se2.printStackTrace();
}
}
}
return rowCount;
}
当我运行上面的代码时,我得到以下异常:
Exception occured in the database
java.sql.SQLException: Unsupported feature
at oracle.jdbc.driver.OraclePreparedStatement.setCursorInternal(OraclePreparedStatement.java:5867)
at oracle.jdbc.driver.OracleCallableStatement.setCursor(OracleCallableStatement.java:5297)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setCursor(OraclePreparedStatementWrapper.java:410)
at com.rolta.HrManager.printMaxSalAllDept(HrManager.java:1038)
at com.rolta.HrManager.main(HrManager.java:1344)
Exception message: Unsupported feature
Database error code: 17023
我在这个论坛上看到了一些帖子,其他帖子建议更新到最新版本的 JDBC 驱动程序可以解决这个问题。就我而言,我使用的是最新版本的 Oracle JDBC 驱动程序(ojdbc6.jarOracle Database 11g Release 2 11.2.0.4 JDBC Drivers 下的第一个 jar)。所以我不认为它是导致问题的版本。
如果我所做的事情是非法的,那么抛出的异常消息就会表明这一点。 但这里有“不支持的功能”消息,似乎此功能不适用于我的数据库(或其我正在使用的版本 (11g) )或我正在使用的 JDBC 驱动程序版本。这是对这个例外的正确解释吗?
【问题讨论】:
-
您是否在您的数据库文档中查找了错误代码
17023对应的内容? -
您在类路径中添加的 jdbc jar 版本
-
已更新我的帖子以反映我正在使用的 jdbc jar 版本。
-
“不支持的功能”的哪一部分不清楚?另外 11.2.0.4 不是最新的驱动程序,最新的是 12.1.0.2。见oracle.com/technetwork/database/features/jdbc/index-091264.html
-
@MarkRotteveel 但我使用的是 oracle 11g 和 Java 1,6。这就是为什么我选择了帖子中提到的罐子。
标签: java exception jdbc oracle11g