2017-06-11 14 views
1

私は現地でSakila Sample Databaseを持っており、get_customer_balance関数をJavaから呼び出そうとしています。JavaストアドファンクションをJavaから呼び出すにはどうすればよいですか?

これは私が持っているコードです:

になり
double callableStatementFunctionCallExample(final int customerId) throws SQLException { 
    double customerBalance = -1; 

    final Connection connection = DriverManager.getConnection(url, user, pass); 

    final CallableStatement callableStatement = connection.prepareCall("{CALL ? = get_customer_balance(?, ?)}"); 
    callableStatement.registerOutParameter(1, customerBalance); 
    callableStatement.setInt(2, customerId); 
    final java.sql.Date date = new Date(Calendar.getInstance().getTimeInMillis()); 
    callableStatement.setDate(3, date); 

    callableStatement.execute(); 
    return customerBalance; 
} 

..

callableStatement.setDouble(1, customerBalance); 
Exception in thread "main" java.sql.SQLException: Parameter number 1 is not an OUT parameter. 

だから私はこの文を置き換え..

callableStatement.registerOutParameter(1, customerBalance); 

になり

:だから明らかに私はout parameterを登録する必要がありますが、どのように

Exception in thread "main" java.sql.SQLException: Parameter p_film_count is not registered as an output parameter. 

?なぜ私の最初のアプローチが間違っていますか?

答えて

1

署名sqlTypeが「java.sql.Typesによって定義されるJDBCタイプコード」と記載されているvoid registerOutParameter(int parameterIndex, int sqlType)、です。

callableStatement.registerOutParameter(1, Types.DOUBLE); 

そして、あなたが使用して​​呼び出した後、その値を取得します:

double customerBalance = callableStatement.getDouble(1); 

あなたはTypes.LONGVARCHARの値である値-1、とそれを呼んでいたあなたの呼び出しがなければならないことを意味し


また、CallableStatementのjavadocがSQL文字列の構文は、あなたの文がなければならないことを意味しており、{?= call <procedure-name>[(<arg1>,<arg2>, ...)]}であることを示しています

final CallableStatement callableStatement = connection.prepareCall("{? = CALL get_customer_balance(?, ?)}"); 

あなたがしたい場合は両方の問題が修正されている可能性がjavadocを読んでください。

+0

まだ取得しています:スレッド "main"の例外java.sql.SQLException:パラメータ番号1はOUTパラメータではありません。 –

0

前に実行()タイプ(例えば、整数)で登録します方法の

final CallableStatement callableStatement = connection.prepareCall("{? = call get_customer_balance(?, ?)}"); 

callableStatement.registerOutParameter(1, java.sql.Types.INTEGER); 
+0

まだ取得中:パラメータ番号1はOUTパラメータではありません。 –

+0

connection.prepareCallを更新してください – user7294900

関連する問題