结果集的存储过程 Returns 为 null
Stored Procedure Returns null to Result Set
在处理 JDBC 时遇到问题 我在 mysql 中创建了一个 table,其中包含员工信息 Employee_Id、姓名、城市、薪水和Year_of_joining 并且我还为同一个数据库创建了一个存储过程,它接受两个输入并根据加入年份更新员工的工资。这是存储过程:
CREATE PROCEDURE proc(IN var1, IN var2)
BEGIN
UPDATE "TABLE" SET Salary=var1+Salary WHERE Year_of_Joining= var2
END
现在我通过 java 到 mysql 数据库创建了一个数据库连接(该连接已成功建立)
但是现在,当我在创建 CallableStatement 对象后执行查询并尝试将可调用对象的当前结果转换为结果集时,我得到了 null 。请帮我解决一下这个。
这是代码:
// Creates connection with java:
Connection con= DriverManager.getConnection(url,username,pswrd);
// Now Calling the Stored procedure from mysql
CallableStatement col = con.prepareCall("{call proc(?,?)}");
// gave values to the procedure
col.setDouble(1,20000);
col.setInt(2,2012);
col.execute();
// Returning Result set=null
ResultSet rs = col.getResultSet();
- Firstly, you have not set the output parameter in your stored procedure.
Like :
CREATE PROCEDURE proc(IN inputParam VARCHAR(255), OUT OutParam INT)...
Then, to retrieve the values of output parameters (parameters
specified as OUT or INOUT when you created the stored procedure), JDBC
requires that they be specified before statement execution using the
various registerOutputParameter() methods in the CallableStatement
interface.
col.registerOutParameter("OutParam", Types.INTEGER);
- Secondly, UPDATE statements don't return a ResultSet! Only SELECT statements return a ResultSet to operate upon.
在处理 JDBC 时遇到问题 我在 mysql 中创建了一个 table,其中包含员工信息 Employee_Id、姓名、城市、薪水和Year_of_joining 并且我还为同一个数据库创建了一个存储过程,它接受两个输入并根据加入年份更新员工的工资。这是存储过程:
CREATE PROCEDURE proc(IN var1, IN var2)
BEGIN
UPDATE "TABLE" SET Salary=var1+Salary WHERE Year_of_Joining= var2
END
现在我通过 java 到 mysql 数据库创建了一个数据库连接(该连接已成功建立)
但是现在,当我在创建 CallableStatement 对象后执行查询并尝试将可调用对象的当前结果转换为结果集时,我得到了 null 。请帮我解决一下这个。 这是代码:
// Creates connection with java:
Connection con= DriverManager.getConnection(url,username,pswrd);
// Now Calling the Stored procedure from mysql
CallableStatement col = con.prepareCall("{call proc(?,?)}");
// gave values to the procedure
col.setDouble(1,20000);
col.setInt(2,2012);
col.execute();
// Returning Result set=null
ResultSet rs = col.getResultSet();
- Firstly, you have not set the output parameter in your stored procedure.
Like :
CREATE PROCEDURE proc(IN inputParam VARCHAR(255), OUT OutParam INT)...
Then, to retrieve the values of output parameters (parameters specified as OUT or INOUT when you created the stored procedure), JDBC requires that they be specified before statement execution using the various registerOutputParameter() methods in the CallableStatement interface.
col.registerOutParameter("OutParam", Types.INTEGER);
- Secondly, UPDATE statements don't return a ResultSet! Only SELECT statements return a ResultSet to operate upon.