# 5.16 Calling Stored Procedures

The Db utility class makes it easy to call stored procedures. Below is a code example:

Db.execute((connection) -> {
    CallableStatement cs = connection.prepareCall(...);
    cs.setObject(1, ...);
    cs.setObject(2, ...);
    cs.execute();
    cs.close();
    return cs.getObject(1);
});
1
2
3
4
5
6
7
8

As shown above, you can easily call a stored procedure using Db.execute(...). The connection is an object of type Connection. After you are done using it, you don't need to call close() on it, as JFinal will automatically close it for you at the higher level.

Moreover, under MySQL, you can use an even simpler way to call stored procedures:

// Call stored procedure to query the salary table
Record first = Db.findFirst("CALL FindSalary (1,\"201901\")");

// Call stored procedure to insert into the salary table
int update2 = Db.update("CALL InsertSalary (3, 123)");

// Call stored procedure to update the salary table
int update = Db.update("CALL UpdateSalary (3, 99999)");

// Call stored procedure to delete from the salary table
int delete = Db.delete("CALL DeleteSalary(3)");
1
2
3
4
5
6
7
8
9
10
11

These examples demonstrate how to execute various database operations—select, insert, update, and delete—by calling stored procedures in MySQL. The code is quite straightforward and takes advantage of JFinal's Db utility class for simplifying the operations.

Last Updated: 9/21/2023, 8:42:09 AM