Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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 CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

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);

parameters.apply(statement);

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.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
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 }}