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

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

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.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}