使用Java记录PreparedStatements [英] Logging PreparedStatements in Java

查看:126
本文介绍了使用Java记录PreparedStatements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一件事总是令人痛苦的是,当你有PreparedStatement而不是查询本身时,记录SQL(JDBC)错误。

One thing that always been a pain is to log SQL (JDBC) errors when you have a PreparedStatement instead of the query itself.

你总是最终得到消息喜欢:

You always end up with messages like:

2008-10-20 09:19:48,114 ERROR LoggingQueueConsumer-52 [Logger.error:168] Error 
executing SQL: [INSERT INTO private_rooms_bans (room_id, name, user_id, msisdn, 
nickname) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE room_id = ?, name = ?, 
user_id = ?, msisdn = ?, nickname = ?]

当然我可以写一个帮手检索值并使用实际值解析/替换问号的方法(如果我没有得到这个问题的结果,可能会沿着那条路走下去),但我只是想知道这个问题是否已经被某人解决了else和/或是否有任何通用的日志记录助手可以自动为我做。

Of course I could write a helper method for retrieving the values and parsing/substitute the question marks with real values (and probably will go down that path if I don't get an outcome of this question), but I just wanted to know if this problem was resolved before by someone else and/or if is there any generic logging helper that would do that automagically for me.

在几个答案后编辑:

到目前为止提供的库似乎适合于记录调试语句,这无疑是有用的。但是,我正在寻找一种方法来获取PreparedStatement本身(不是某些子类)并在发生错误时记录其SQL语句。我不想部署具有PreparedStatement备用实现的生产应用程序。

The libraries provided so far seems to be suitable to logging the statements for debugging, which no doubt is useful. However, I am looking to a way of taking a PreparedStatement itself (not some subclass) and logging its SQL statement whenever an error occur. I wouldn't like to deploy a production app with an alternate implementation of PreparedStatement.

我想我正在寻找一个实用工具类,而不是PreparedStatement专业化。

I guess what I am looking for an utility class, not a PreparedStatement specialization.

谢谢!

推荐答案

我试过 log4jdbc 它完成了我的工作。

I tried log4jdbc and it did the job for me.

安全注意:截至2011年8月,log4jdbc准备好的语句的记录结果执行起来并不安全。它们可用于分析,但绝不应反馈到DBMS。

logjdbc生成的日志示例


2010/08/12 16:30:56 jdbc.sqlonly
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate (DelegatingPreparedStatement.java:105)
8.插入A_TABLE
(ID_FILE,CODE1,ID_G,ID_SEQUENCE,REF,NAME,BAR,DRINK_ID,AMOUNT,DESCRIPTION,STATUS,CODE2,REJECT_DESCR,ID_CUST_REJ)
VALUES
(2,'123',1,'2','aa','awe',null,'0123',4317.95,'Rccc','0',null,null,null )

2010/08/12 16:30:56 jdbc.sqlonly org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) 8. INSERT INTO A_TABLE (ID_FILE,CODE1,ID_G,ID_SEQUENCE,REF,NAME,BAR,DRINK_ID,AMOUNT,DESCRIPTION,STATUS,CODE2,REJECT_DESCR,ID_CUST_REJ) VALUES (2,'123',1,'2','aa','awe',null,'0123',4317.95,'Rccc','0',null,null,null)

该库非常容易设置:

我的配置 HSQLDB

jdbc.url=jdbc:log4jdbc:hsqldb:mem:sample

使用 Oracle

jdbc.url=jdbc:log4jdbc:oracle:thin:@mybdd:1521:smt
jdbc.driverClass=net.sf.log4jdbc.DriverSpy

logback.xml:

logback.xml :

<logger name="jdbc.sqlonly" level="DEBUG"/>

太糟糕了,它不在maven存储库上,但仍然有用。

根据我的尝试,如果你设置

Too bad it wasn't on a maven repository, but still useful.
From what I tried, if you set

你只会错误地得到这些陈述,我不知道这个库是否会对性能产生影响。

You will only get the statements in error, however, I don't know if this library has an impact on performance.

这篇关于使用Java记录PreparedStatements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆