Java - MySQL 调用存储过程

标签 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();

抛出这个异常;

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/

相关文章:

mysql - 从数据库在 Prestashop 中创建产品

php - Laravel Eloquent - 混合了 snake_case 和 camelCase 列的现有数据库

mysql - #1111 - 存储过程 mysql 中组函数错误的无效使用

Mysql In 子句选择所有记录

MySQL : transaction within a stored procedure

java: NoSuchElementException: 读取 arrayList

java - 为什么我无法在 mouseEntered 方法中获取 JButton 的名称?

java - 无法将类型 [java.lang.String] 的值转换为所需类型 [org.javatuples.Pair] : no matching editors or conversion strategy found

php - 表别名+左连接

java - Tomcat错误信息 : java. lang.ClassNotFoundException : listeners. ContextListener/SessionListener/websocket.drawboard.DrawboardContextListener