我正在尝试从 Java 调用存储过程。但是,我所做的是寻找一个函数。我错过了什么?以下是我目前尝试过的方法;
open();
try {
statement = conn.prepareStatement(StringConstant.PROC_GET_POSITIONS);
ResultSet resultSet = statement.executeQuery();
while( resultSet.next() ){
System.out.println(resultSet.getString(0));
}
} catch ( SQLException sqlEx ) {
sqlEx.printStackTrace();
}
close();
抛出这个异常;
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: FUNCTION test.get_positions does not exist
存储过程是这样写的(这是为了测试):
CREATE FUNCTION get_positions(OUT o_position VARCHAR(25))
BEGIN
SELECT pos_name
INTO o_position
FROM master_position;
END;
我提出这个问题的原因是 SO 建议了这个标题:
-自动调用存储过程1
-如何在MySQL 1的存储过程中调用存储过程
-在Hibernate 2中调用存储过程
- 从存储过程调用存储过程和/或使用 COUNT 2
-mysql存储过程调用hibernate 2
这些都没有回答我的问题。
最佳答案
我现在遇到了这个问题。我发现了这个:
Connection conn = getMySqlConnection();
// Step-2: identify the stored procedure
String simpleProc = "{ call simpleproc(?) }";
// Step-3: prepare the callable statement
CallableStatement cs = conn.prepareCall(simpleProc);
// Step-4: register output parameters ...
cs.registerOutParameter(1, java.sql.Types.INTEGER);
// Step-5: execute the stored procedures: proc3
cs.execute();
// Step-6: extract the output parameters
int param1 = cs.getInt(1);
System.out.println("param1=" + param1);
我认为这可能是一个很好的例子。
来源:http://www.java2s.com/Code/Java/Database-SQL-JDBC/CallStoredProcedureInMySql.htm
关于Java - MySQL 调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16708129/