ca.krasnay.sqlbuilder.ParameterizedPreparedStatementCreator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sqlbuilder Show documentation
Show all versions of sqlbuilder Show documentation
A lightweight library for building and executing SQL statements.
The newest version!
package ca.krasnay.sqlbuilder;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.PreparedStatementCreator;
/**
* Implementation of Spring's PreparedStatementCreator that allows for the
* substitution of named parameters. Here's a simple example:
*
*
* PreparedStatement ps = new ParameterizedPreparedStatementCreator().setSql(
* "select * from Employee where name like :name").setParameter("name", "Bob%").createPreparedStatement(conn);
*
*
* The implementation performs simple text substitution of parameters, which can
* lead to problems with things like this:
* "select * from Employee where name = 'foo:bar'" In this case,
* {@link #createPreparedStatement(Connection)} will fail if the
* bar
is not defined, or will result in
* "select * from Employee where name = 'foo?'", both of which are wrong.
*
* @author John Krasnay
*
*/
public class ParameterizedPreparedStatementCreator implements Cloneable, PreparedStatementCreator, Serializable {
static class SqlAndParams {
private String sql;
private List