用p6spy监控你的jdbc数据库操作

需要包

P6SPY    http://p6spy.sourceforge.net

 

spy.properties

################################################################################
module.log=com.p6spy.engine.logging.P6LogFactory

realdriver=org.gjt.mm.mysql.Driver

#specifies another driver to use
realdriver2=
#specifies a third driver to use
realdriver3=

deregisterdrivers=false

executionthreshold=

outagedetection=false
outagedetectioninterval=


# filter what is logged
filter=false

include     =

exclude     =

sqlexpression =


# turn on tracing
autoflush   = true

dateformat=


includecategories=

#list of categories to exclude: error, info, batch, debug, statement,
#commit, rollback and result are valid values
excludecategories=info,debug,result,batch

 

stringmatcher=

# prints a stack trace for every statement logged
stacktrace=false
# if stacktrace=true, specifies the stack trace to print
stacktraceclass=

# determines if property file should be reloaded
reloadproperties=false
# determines how often should be reloaded in seconds
reloadpropertiesinterval=60

#if=true then url must be prefixed with p6spy:
useprefix=false


appender=com.p6spy.engine.logging.appender.FileLogger

# name of logfile to use, note Windows users should make sure to use forward slashes in their pathname (e:/test/spy.log) (used for file logger only)
logfile     = spy.log


append=true

#The following are for log4j logging only
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=p6spy - %m%n


log4j.logger.p6spy=INFO,STDOUT

 

################################################################################

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

/*
 * 项目名称:  2006-3-4
 */

public class TestConnMysqlWithSpy {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            Class.forName("com.p6spy.engine.spy.P6SpyDriver").newInstance();

            }
            catch (Exception E) {
            System.err.println("Unable to load driver.");

            }

            try {
            Connection C = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=&useUnicode=true&characterEncoding=GBK");
            Statement s=C.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet rs=s.executeQuery("select * from abc");
   
            int i=1;
            for (i=10;i<20;i++)
            {
            if (rs.next())
            {

            System.err.println(rs.getString(1));

            }
         

            }
     
            rs.close();
            s.close();
            C.close();


            }
            catch (SQLException E) {
            System.err.println("SQLException: " + E.getMessage());
            System.err.println("SQLState: " + E.getSQLState());
            System.err.println("VendorError: " + E.getErrorCode());
            }

       
       

    }

}

下载
 

你可能感兴趣的:(数据库处理)