简体   繁体   中英

Debugging SQL Queries with 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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM