Search This Blog

Friday, October 21, 2016

P6 Spy with a stand alone jdbc program

P6 Spy with a stand alone jdbc program
package mdsleak;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Class1 {
    public Class1() {
        super();
    }

    public static void main(String[] argv) {

                    System.out.println("-------- Oracle JDBC Connection Testing ------");

                    try {

                         //  Class.forName("oracle.jdbc.driver.OracleDriver");
                        Class.forName("com.p6spy.engine.spy.P6SpyDriver");

                    } catch (ClassNotFoundException e) {

                            System.out.println("Where is your Oracle JDBC Driver?");
                            e.printStackTrace();
                            return;

                    }

                    System.out.println("Oracle JDBC Driver Registered!");

                    Connection connection = null;

                    try {

                       //     connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","tiger");
                       connection = DriverManager.getConnection("jdbc:p6spy:oracle:thin:@localhost :1521:orcl", "scott","tiger");

                        Statement stmt3 = connection.createStatement();
                        ResultSet rs3 = stmt3.executeQuery("SELECT COUNT(*) FROM EMP,EMP,EMP,EMP,EMP");
                            while(rs3.next()){
                            System.out.println(rs3.getInt(1));
                            }

                    } catch (SQLException e) {

                            System.out.println("Connection Failed! Check output console");
                            e.printStackTrace();
                            return;

                    }

                    if (connection != null) {
                            System.out.println("You made it, take control your database now!");
                    } else {
                            System.out.println("Failed to make connection!");
                    }
            }

}


Create a directory c:\temp\p6spy

Copy the file p6spy.jar to c:\temp\p6spy

Create a new file c:\temp\p6spy\spy.properties with the following
contents

++++++++++
realdatasourceclass=oracle.jdbc.driver.OracleDriver
autoflush=true
logfile=c:/temp/spy.log
append=false
+++++++++

Run the Java Program with the following arguments

C:\jdk1.7.0_79\bin\java.exe -classpath D:\myjdevwork\jdev1117\mywork\Application2\MDSLeak\classes;c:\temp\p6spy;c:\temp\p6spy\p6spy.jar;D:\sqldeveloper\jdbc\lib\ojdbc7.jar Class1


 you should see a file c:/temp/spy.log with the following

1477027846045|193|statement|connection 0|SELECT COUNT(*) FROM EMP,EMP,EMP,EMP,EMP|

Here 193 is the time it took in ms and 1477027846045 is the The Current Unix Timestamp

Import changes to the JDBC Program 

 //  Class.forName("oracle.jdbc.driver.OracleDriver");
  Class.forName("com.p6spy.engine.spy.P6SpyDriver");

   //     connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","tiger");
         connection = DriverManager.getConnection("jdbc:p6spy:oracle:thin:@localhost :1521:orcl", "scott","tiger");




Tags: Publish
October 21, 2016 at 12:20PM
Open in Evernote

No comments: