How to use P6Spy in a simple JDBC Application


This article demonstrates how to use P6Spy in a simple JDBC application

Software Environment

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)
  • MySQL 5.6.16 – Community Server (GPL)
  • MySQL Java Connector 5.1.34
    • Referenced mysql-connector-java-5.1.34-bin.jar in Eclipse
  • P6Spy 2.1.2
    • Download from https://github.com/p6spy/p6spy/releases
    • Referenced p6spy-2.1.2.jar in Eclipse
  • SLF4J 1.7.7
    • Referenced the following in Eclipse
      • slf4j-api-1.7.7.jar
      • slf4j-log4j12-1.7.7.jar
      • slf4j-simple-1.7.7.jar
  • Log4j2 2.0.2
    • Reference the following in Eclipse
      • log4j-core-2.0.2.jar
      • log4j-api-2.0.2.jar
      • log4j-1.2-api-2.1.jar (Yes, it is in 2.0.2 release)

Data Set Up



Java Codes

Eclipse Workspace


Sample Output

When you run MySqlJdbcSample.java within Eclipse, the logs are written in logs/name.log file. Based on the log4j2.xml, the file is rotated once the size of 1 MB is reached. Here is a sample output:

Download the Project



Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert