Java - MySQL调用存储过程

Mic*_*dan 1 java mysql stored-procedures callable-statement

我正在尝试从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();
Run Code Online (Sandbox Code Playgroud)

抛出这个例外;

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: FUNCTION test.get_positions does not exist
Run Code Online (Sandbox Code Playgroud)

这是存储过程的编写方式(这是为了测试):

CREATE FUNCTION get_positions(OUT o_position VARCHAR(25))
BEGIN

  SELECT pos_name
  INTO o_position
  FROM master_position;

END;
Run Code Online (Sandbox Code Playgroud)

我提出这个问题的原因是SO建议这个标题:

-automate调用存储过程1
- 如何在MySQL中的存储过程中调用存储过程1
在Hibernate中 调用存储过程
2
从存储过程中调用存储过程和/或使用COUNT 2 -mysql存储过程调用hibernate 2

没有人回答我的问题.

Jes*_*sus 8

我知道这个问题.我找到了这个:

    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);
Run Code Online (Sandbox Code Playgroud)

我认为这可能是一个很好的例子.

再见!

资料来源:http://www.java2s.com/Code/Java/Database-SQL-JDBC/CallStoredProcedureInMySql.htm