[英]Java - MySQL Calling Stored Procedure
我正在尝试从 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
您正在尝试从存储过程中获取结果。 存储过程不返回值,而是 Function 。
Statement stmt;
stmt = conn.createStatement();
stmt.execute(String.format("CALL swap_devices(%d,%d)", oldDeviceID, newDeviceID));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.