이것은 비열한 우회하는 문제입니다 완전히는 대처하기 매우 쉽습니다 :
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);
프레스토 :
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;
}
}
은 무효 preparePrepared을 무시 익명 클래스 (PreparedStatement로)를 작성, 사용하기! 이제는 PreparedStatement를 작성하지 않고 작업 할 수있는 방법이 있습니다. 다음은 다른 나라에 붙여 올 복사해야 할 것이다 최소한의 상용구를 보여주는 예입니다, 당신이 원하는 때마다 다른 문 쓰고 : 그 사방
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); }
}
}
당신은 정말 정말되고 싶지 않아 복사 붙여 넣기, 너?
이 라이브러리는 포함되지 않은 "com.healthmarketscience.common.util"패키지를 가져옵니다. – r3zn1k
조금만 돌아 가야합니다. http://openhms.sourceforge.net/common-util/ – PeterMmm
최고의 솔루션. 많은 감사합니다! – r3zn1k