Subject Re: [Firebird-Java] Select with constant value
Author Roman Rokytskyy
> Now, I'm preparing this sort of query directly appending the constant
> value to the sql string. So it looks like:
>
> int const;
>
> String str = "SELECT FIELD1, " + const + " AS CONST, FIELD2 ";
> str += "FROM TABLE1 ";
> str += "WHERE FIELD1 = ?";
>
> pst = conn.prepareStatement(str);
> pst.setInt(1, val);
> pst.execute();
>
> Am I loosing the prepared statement's befenefits in this situation? The
> const value will change in every new query.

You're loosing it by calling conn.prepareStatement(String). In general each
call to Connection.prepareStatement(String) tells server to compile new
statement. However, some connection pools (including JayBird
FBWrappingDataSource) still cache prepared statements by comparing the SQL
being prepared. By appending some changing value you loose this feature too.

> The same question arises related to the "SELECT FIRST x SKIP y" clause.
> In my application I use the same way, appending to the query string the
> desired constant values, to make that kind of queries.

This should work without any problems, i.e. SELECT FIRST ? SKIP ? * FROM
myTable ORDER BY myCol.

Roman