简体   繁体   English

使用Spring JDBC调试SQL查询

[英]Debugging SQL Queries with Spring JDBC

I'm working with the Spring Framework, and I'm following a test driven development. 我正在使用Spring Framework,我正在进行测试驱动的开发。 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. 我得到一个例外,但我不完全确定为什么所以我想看看查询jdbc实际上是在运行什么。 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? 那么,查看JDBC运行的查询或获取一些有用信息的最佳方法是什么? It currently throws org/springframework/dao/QueryTimeoutException (which I find infinitely unhelpful) so I don't really know what could be going wrong. 它目前抛出org/springframework/dao/QueryTimeoutException (我发现无限无益)所以我真的不知道会出现什么问题。

EDIT: Have now added log4j but still not getting a useful query. 编辑:现在添加了log4j但仍未获得有用的查询。 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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