Skip to content

How to display all MySQL queries your application generates.

Sometimes it’s a good idea to intercept the SQL queries exactly as they are generated by your Java application. One way to do it (in MySQL) is to enable general_log. Another really handy option is to use MySQL JDBC driver option: autoGenerateTestcaseScript. It will dump all the queries to standard error, as they are being sent to the database.

com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
ds.setAutoGenerateTestcaseScript(true);
ds.setURL("jdbc:mysql://host:port/dbname");
Connection con = ds.getConnection("user", "password");
//...

This will give you on your stderr output like this:


/* conn id 384 */ SELECT * FROM user WHERE id = 2;
/* conn id 384 */ SELECT * FROM user WHERE id = 1;

Post a Comment

Your email is never published nor shared.
*