How to enable JDBC tracing?

A good way to find out what JDBC calls are doing is to enable JDBC tracing. The JDBC trace contains a detailed listing of the activity occurring in the system that is related to JDBC operations.

If you use the DriverManager facility to establish your database connection, you use the DriverManager.setLogWriter method to enable tracing of JDBC operations.

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Program {

public static void main(String... args) {
Connection con = null;

try {
DriverManager.setLogWriter(new PrintWriter(System.out));
catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
return ;

try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb",
"usr", "sql");

System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");

} catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
catch(SQLException e) {

If you use a DataSource object to connect to a data source, you use the DataSource.setLogWriter method to enable tracing. (For pooled connections, you use the ConnectionPoolDataSource.setLogWriter method, and for connections that can participate in distributed transactions, you use the XADataSource.setLogWriter method.)

Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |