Named Prepared Statements in C5 User Core


Let’s consider a normal JAVA prepared statement.

PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?");
pstmt.setBigDecimal(1, 153833.00)
pstmt.setInt(2, 110592)
"SELECT * FROM Users WHERE username IN (?, ?, ?)"


In C5 User core we are using NamedPreparedStatement class to overcome these issues. NamedPreparedStatement is a extended version of JAVA PreparedStatement. It has the ability to use named placeholders in the SQL query instead of using “?” as the placeholder. Below is the modified version of above example using NamedPreparedStatement.

NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, "UPDATE EMPLOYEES SET SALARY = :salary; WHERE ID = :id;");
namedPreparedStatement.setBigDecimal("salary", 153833.00);
namedPreparedStatement.setInt("id", 110592);
"SELECT * FROM Users WHERE username IN (:usernames;)"
List<String> listOfUsernames = new ArrayList<>();
namedPreparedStatement.setString("usernames", listOfUsernames)

Software Engineer @WSO2