to nikczemnego przebiegły problemu, dziękuję w pełni to dość łatwe do radzenia sobie z:
public class PreparedStatementBuilder
{
private String sql; // the sql to be executed
public PreparedStatementBuilder(final String sql) { this.sql = sql; }
protected void preparePrepared(final PreparedStatement preparedStatement)
throws SQLException
{
// this virtual method lets us declare how, when we do generate our
// PreparedStatement, we want it to be setup.
// note that at the time this method is overridden, the
// PreparedStatement has not yet been created.
}
public PreparedStatement build(final Connection conn)
throws SQLException
{
// fetch the PreparedStatement
final PreparedStatement returnable = conn.prepareStatement(sql);
// perform our setup directives
preparePrepared(returnable);
return returnable;
}
}
Aby skorzystać, wystarczy napisać anonimowy klasy, który zastępuje void preparePrepared (PreparedStatement):
final String sql = "SELECT * FROM FOO WHERE USER = ?";
PreparedStatementBuilder psBuilder = new PreparedStatementBuilder(sql){
@Override
protected void preparePrepared(PreparedStatement preparedStatement)
throws SQLException
{
preparedStatement.setString(1, "randal");
}};
return obtainResultSet(psBuilder);
Presto! Masz teraz sposób na pracę z PreparedStatement bez zbudowania go. Oto przykład pokazujący minimalne boilerplate chcesz inaczej trzeba skopiować pasta do królestwa przyjść, za każdym razem chciałem napisać inny oświadczenie:
public ResultSet obtainResultSet(final PreparedStatementBuilder builder)
throws SQLException {
final Connection conn = this.connectionSource.getConnection();
try
{
// your "virtual" preparePrepared is called here, doing the work
// you've laid out for your PreparedStatement now that it's time
// to actually build it.
return builder.build(conn).executeQuery();
}
finally
{
try { conn.close(); }
catch (SQLException e) { log.error("f7u12!", e); }
}
}
Naprawdę naprawdę nie chcą być kopia wklejanie, że wszędzie, czy ty?
Ta biblioteka importuje pakiet "com.healthmarketscience.common.util", który nie jest dołączony ... – r3zn1k
Musisz trochę przejść: http://openhms.sourceforge.net/common-util/ – PeterMmm
Najlepsze rozwiązanie. Bardzo dziękuję! – r3zn1k