Amir Hasan Amir Hasan - 2 months ago 6x
SQL Question

Error in JDBC code calling a stored function which returns a value

I'm trying to print the returned value of a MySQL stored function from the JDBC code which is as follows (I am using MySQL Server 5.7.13):

package jdbc;

import java.sql.DriverManager;
import java.sql.*;
import java.util.Scanner;

public class CallableStatementsCallingFunctions {
public static void main(String... syrt)
catch(ClassNotFoundException e)
System.out.println("Error(class): "+ e);
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/collablestatement","root","mysql") ;
CallableStatement cs = conn.prepareCall("{call ?:=getBalance1(?)}");
String s = new Scanner(;
System.out.println("Account number :" + cs.getInt(1));
catch(SQLException e)
System.out.println("Error(SQL) : "+e);
catch(Exception e)
System.out.println("Error(Fro outer try) : "+ e);


the stored function
is shown here

getBalance(acno numeric)

my code output is shown here

jdbc output

I am getting the output from the SQL command but in JDBC I am getting and
saying that

parameter 1 is not an output parameter

I know that parameter 1 has been used as the placeholder of the returned value from the function in jdbc code. In
I also tried the syntax -
{?:= call getBalance1(?)}
, but even then getting the same Exception.

Why am I getting the exception?


getBalance1() is a MySQL FUNCTION, not a PROCEDURE, so I wouldn't expect using a JDBC CallableStatement to be applicable.

Even in your MySQL console test you are using

select getBalance1(103)

so you simply need to do the same thing in your Java code using a PreparedStatement:

PreparedStatement ps = conn.prepareStatement("select getBalance1(?)");
ps.setInt(1) = 103;
ResultSet rs = ps.executeQuery();;
Double bal = rs.getDouble(1);

(It should be noted that since "balance" apparently refers to "money", REAL is not a good choice for the column type; details here.)