Sar009
Sar009

Reputation: 2276

generalise and log SQL query in java

I am using JDBI to query MySQL in my java app. I want to log the metrics around the queries that are being performed on the app such as time spent by a query, the number of times a query was executed, time spend to obtain handle etc.. Although most of my queries are performed through a prepared statement, I have some queries where I pass the parameters in the query as part of SQL string. for example

select * from products where category in ('a', 'b', 'c') and id = :id;

JDBI logger outputs it as

select * from products where category in ('a', 'b', 'c') and id = ?;

it becomes very hard to group multiple queries to analyse it. Is there a library in java which can help me generalise such queries to like

select * from products where category in (?) and id = ?;

or maybe

select * from products where category in (?, ?, ?) and id = ?;

P.S. I know it is a really bad design to concat a string in SQL queries.

Upvotes: 0

Views: 409

Answers (1)

Rick James
Rick James

Reputation: 142296

In MySQL, there are two approaches, both of which seem to meet your desires:

I think both will do in (?, ?, ?) and id = ?, and they use that as a "digest" of the query to summarize similar queries as to elapsed time, etc.

Upvotes: 0

Related Questions