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

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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.

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

java ,jdbc ,prepared statements

Published at DZone with permission of Greg Brown, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}