How to Enable JDBC Logging in Java Code?

Below java code shows, how to enable JDBC logging in the code –

/****************************************************************************************
 * Created on 04-2011 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.javasql;

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

/**
 * Created by http://kodehelp.com
 * Date: 4/29/11
 */
public class EnableJDBCLogging {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        Class.forName("oracle.jdbc.driver.OracleDriver");
        final String URL = "jdbc:oracle:thin:@HOST:PORT:SID";
        Connection dbConn = DriverManager.getConnection(URL, "user", "pass");
        PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
        DriverManager.setLogWriter(w);
        dbConn.close();

        PreparedStatement prepstmt;
        prepstmt = dbConn.prepareStatement("SELECT id FROM employee");
        prepstmt.execute();
        prepstmt.close();
        dbConn.close();
    }
}

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.