Invoking Function and Stored Procedure using JDBC
import java.sql.*;
public class FunctionCallExample {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:3306/mydatabase";
String username = "username";
String password = "password";
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
// Calling a Function
String functionCall = "{ ? = call my_function() }";
try (CallableStatement callableStatement = connection.prepareCall(functionCall)) {
callableStatement.registerOutParameter(1, Types.INTEGER); // Assuming the function returns an integer
callableStatement.execute();
int result = callableStatement.getInt(1); // Retrieve the returned value
System.out.println("Result from the function: " + result);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}Last updated