Over a million developers have joined DZone.

HTTP-RPC Parameters Class

DZone 's Guide to

HTTP-RPC Parameters Class

See how the new Parameters class in the HTTP-RPC Java Server library speeds up coding.

· Java Zone ·
Free Resource

A new Parameters class has been added to the HTTP-RPC Java server library. This class provides a means for executing prepared JDBC statements using named parameter values rather than indexed arguments. Parameter names are specified by a leading : character, similar to the named parameter syntax used by the Java Persistence API (JPA). For example:

SELECT * FROM some_table 
WHERE column_a = :a OR column_b = :b OR column_c = COALESCE(:c, 4.0)

The parse() method is used to create a Parameters instance from a SQL statement. It takes a java.io.Reader containing the SQL text as an argument:

Parameters parameters = Parameters.parse(new StringReader(sql));

The getSQL() method of the Parameters class returns the parsed SQL in standard JDBC syntax; for example:

SELECT * FROM some_table 
WHERE column_a = ? OR column_b = ? OR column_c = COALESCE(?, 4.0)

This value is used to create the actual prepared statement:

PreparedStatement statement = DriverManager.getConnection(url).prepareStatement(parameters.getSQL());

Arguments are specified via the arguments map and are applied via the apply() method:

parameters.getArguments().put("a", "hello");
parameters.getArguments().put("b", 3);


Once applied, the statement can be executed:

return new ResultSetAdapter(statement.executeQuery());    

This class has no dependencies, so it can also be used in source code form outside of HTTP-RPC. See the project documentation for more information.

java ,jdbc ,prepared statements

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}