Running prepared SQL queries in jdbc postgres driver

I want to write all prepared sql statements in my java application. I am using the standard postgres jdbc org.postgresql.Driver driver. This driver has a parameter called "loglevel", which can be set to 1 (INFO) or 2 (DEBUG). The point is that the parameter is set to 1, it writes almost nothing, if set to 2, it is too much like

... 20:59:05.608 (2) FE=> Bind(stmt=null,portal=null,$1=<'5'>,$2=<'13'>) 20:59:05.609 (2) FE=> Describe(portal=null) 20:59:05.609 (2) FE=> Execute(portal=null,limit=1) 20:59:05.609 (2) FE=> Sync 20:59:05.648 (2) <=BE ParseComplete [null] 20:59:05.649 (2) <=BE BindComplete [null] 20:59:05.649 (2) <=BE NoData 20:59:05.649 (2) <=BE CommandStatus(UPDATE 1) ... 

Is there only way to register operators + parameters?

+6
source share
1 answer

You are fortunate that you are using PostgreSQL. The implementation of the PostgreSQL JDBC PreparedStatement driver (at least with 8.x or something else) has toString() so that you can see the entire SQL statement with all the parameters populated in the right places. So you can just do something like:

 preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, value1); preparedStatement.setString(2, value2); // ... logger.debug(preparedStatement); // Will show entire SQL with all values. 

(where logger is just your logger, for example slf4j / logback or something else)

+7
source

Source: https://habr.com/ru/post/895913/


All Articles