如何从Java中的连接获取查询字符串? [英] How to get query string from connection in Java?

查看:310
本文介绍了如何从Java中的连接获取查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个方法,尝试记录db调用,形式连接到它,在查询后,有很多地方调用方法connect()启动和cleanUp()方法结束,我可以不想修改每个地方。所以顺序如下:

I'm writing a method that tries to log db calls, form connecting to it, to after the query, there are many places that calls the method connect() to start and the cleanUp() method to end, I can't and don't want to modify every place. So the sequence is like this :

  Connection con = ...
  connect();
  s = con.createStatement();
  ResultSet rs = s.executeQuery(" select * from xyz ");
  rs.next();
  cleanUp();

有很多方法使用这个序列,所以我怎么在cleanUp sql查询字符串[在这种情况下:select * from xyz]从所有运行他们的查询的方法,是否有一种方法从con获取该信息,con对象知道它刚刚运行了什么查询? / p>

There are many methods that uses this sequence, so how do I, somehow in cleanUp(), get the sql query string [ in this case : select * from xyz ] from all the methods that run their queries, is there a way to get that info from the "con", does the con object know what query it has just ran ?

推荐答案

我建议你使用一个方面,AspectJ将是完美的这种情况。
有了一个方面,你可以在被监控的方法执行后触发一个行为。
以下是几个您可以检查的示例: http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html https://mathewjhall.wordpress.com/2011/03/31/tracing-java-method-execution-with- aspectj /

I suggest to you to use an aspect, AspectJ would be perfect for this case. With an aspect, you could trigger a behavior once your monitored method has been executed. Here are a couple examples you could check: http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html and https://mathewjhall.wordpress.com/2011/03/31/tracing-java-method-execution-with-aspectj/

这篇关于如何从Java中的连接获取查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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