I want to know what exactly sql query is processed by jdbi sql api for debugging purposes. My interface class is following
public inteface myinteface{
@SqlQuery("select :c1 from tablename where cond = :cd")
String returnMeValue(@Bind("c1") String c1, @Bind("cd") Integer cd);
}
and later called in another class as String result = myinterfaceclassobject.returnMeValue("Name",1);
I am not getting expected answer so I want to see what actually going to the sql query. So is there any method to get the final processed query?
The dropwizard-jdbi3 module provides you with managed access to JDBI, a flexible and modular library for interacting with relational databases via SQL.
Jdbi is not an ORM. There is no session cache, change tracking, "open session in view", or cajoling the library to understand your schema. Instead, Jdbi provides straightforward mapping between SQL and simple tabular data structures.
Jdbi is an open source Java library (Apache license) that uses lambda expressions and reflection to provide a friendlier, higher level interface than JDBC to access the database.
You can log the sql by writing SqlCustomizer.
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizer;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizerFactory;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizingAnnotation;
import org.skife.jdbi.v2.tweak.StatementCustomizer;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SqlStatementCustomizingAnnotation(LogSqlFactory.Factory.class)
public @interface LogSqlFactory {
static class Factory implements SqlStatementCustomizerFactory {
@Override
public SqlStatementCustomizer createForMethod(Annotation annotation, Class sqlObjectType, Method method) {
return null;
}
@Override
public SqlStatementCustomizer createForType(Annotation annotation, Class sqlObjectType) {
return q -> q.addStatementCustomizer(new StatementCustomizer() {
@Override
public void beforeExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException {
System.out.println(stmt.toString());
}
@Override
public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException { }
@Override
public void cleanup(StatementContext ctx) throws SQLException { }
});
}
@Override
public SqlStatementCustomizer createForParameter(Annotation annotation, Class sqlObjectType, Method method, Object arg) {
return null;
}
}
}
Just include this annotation and use this in SqlObject. In your case use this annotation like this,
@LogSqlFactory
public inteface myinteface{
@SqlQuery("select :c1 from tablename where cond = :cd")
String returnMeValue(@Bind("c1") String c1, @Bind("cd") Integer cd);
}
If you use custom loggers for logging, then beforeExecution method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With