Over a million developers have joined DZone.

HTTP-RPC Parameters Class

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

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, 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);

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.

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

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

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}