DbQuery
is an enhanced wrapper for prepared and regular JDBC statements. In the base scenario, it can be used anywhere where JDBC statements would be used. Nevertheless, DbQuery
provides some additional very convenient features.DbQuery
can be created is by providing database connection. Once created, it can be used similarly as JDBC statement is used:autoClose()
enables 'auto-mode' when the very first next action closes the query. Besides displayed methods, there is method: executeCount()
that is made for executing select count
database queries, or any query that returns a long
number in the first result row and column.close()
method, and all the dirty work is done in the behind. When a query created some ResultSet
, it is possible to explicitly close it using closeResultSet()
method. However, this is not mandatory! User may just simply close a query, and the DbQuery
will close all results set that were created by it! As will be shown later, it is even possible to have automatic query closing:)DbQuery
offers named parameters as well.DbQuery
offers the debug mode that will return the same query string, but populated with real values. Such debug query is just a quick-and-dirty preview and not always 100% syntaxly correct (e.g. strings are not escaped, etc), but sufficient for debugging purposes.DbQuery
initializes lazy. Creating an object still doesn't do anything with the database, therefore it can be configured as needed. DbQuery
initializes on first concrete database-related method. Therefore, setting the debug mode (and other configuration) must be done immediately after the DbQuery
object creation.DbQuery
. As said, each method now has two versions: one that works with ordinal parameters and one for named parameters. Moreover, during setting of a parameter, value will be checked, and if it is null
, the setNull
() method will be invoked instead.setBean()
, setMap()
, setObject()
, setObjects()
...setBean()
it is possible to populate query string where parameters are named as bean properties:DbQuery
provides new method setObject()
for setting objects of unknown type as parameters. For that purpose, DbQuery
must resolve the way how to handle provided type and to invoke correct setter method.SqlTypeManager
, manager for all kind of different SqlType
s. Each SqlType
defines how a type is set and get from the database. There is a large amount of already defined types, however, it is easy to add new and more complex ones.DbQuery
supports auto-generated columns:q.setGeneratedKey()
instead of q.setGeneratedColumns()
in the first example, if that sounds better to you :) Please note that some old database drivers does not support this feature (like HSQLDB 1.x).DbQuery
supports calling stored procedures. The result of the stored procedure is encapsulated in DbCallResult
.