Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging SQL Queries with Spring JDBC

Tags:

java

spring

jdbc

I'm working with the Spring Framework, and I'm following a test driven development. I'm getting an exception but I'm not entirely sure why so I'd like to see what the query jdbc is actually running. The attempted query is the following:

public OrderEntity addOrderEntity(OrderEntity orderEntity) {
    String query = "INSERT INTO ORDERS(ID,REVISION,CONTRACT_ID,PROJECT_ID,WORKSITE_ID,DROPZONE_ID,DESCRIPTION_ID,MANAGER_ID,DELIVERY_DATE,VOLUME) VALUES(?,?,?,?,?,?,?,?,?,?)";
    String id = (orderEntity.get_id() != null) ? orderEntity.get_id() : UUID.randomUUID().toString();
    jdbcTemplate.update(id,1,orderEntity.getContractNo(),orderEntity.getProjectID(),orderEntity.getWorksiteID(),orderEntity.getDropzoneID(),orderEntity.getDescriptionID(),orderEntity.getManagerID(),orderEntity.getDeliveryDate(),orderEntity.getVolume());

    return getOrderEntityById(id);
}

So, what's the best way to see what the query JDBC is running or get some useful information? It currently throws org/springframework/dao/QueryTimeoutException (which I find infinitely unhelpful) so I don't really know what could be going wrong.

EDIT: Have now added log4j but still not getting a useful query. Property file is below:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5t] %-5p %c - %m%n
log4j.rootLogger=trace, stdout

log4j.logger.org.springframework.jdbc.core=DEBUG
log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=DEBUG
like image 259
DrugCrazed Avatar asked Mar 11 '14 11:03

DrugCrazed


Video Answer


1 Answers

You can enable tracing of the queries using:

log4j.logger.org.springframework.jdbc.core = TRACE

Especially

log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=TRACE

this will show messages like this:

 TRACE StatementCreatorUtils:206 - Setting SQL statement parameter value: column index 4, parameter value [TheValueWillBeHere]
like image 152
Laabidi Raissi Avatar answered Oct 18 '22 22:10

Laabidi Raissi