How to start the JDBC Debug mode and print the JDBC diagnostic log

Source: Internet
Author: User
Tags stmt

1. Download the debug version number jar PackageThe first step is to download the JDBC Jar package for the debug version number, with the name of the jar package for the debug version number Jdbcx_g.jar (for example, as seen). The jar package for the debug version number, such as oracle11g, is Jdbc6_g.jar.

Click here to download: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
2. Start logging mode the first way: Set the execution environment variable java-doracle.jdbc.trace=true ... (Elipse in Run->run configurations->arguments column) Another way: Control in the program (recommended in this way)

Mbeanserver mbs = Managementfactory.getplatformmbeanserver (); String loader = Thread.CurrentThread (). Getcontextclassloader (). toString (). ReplaceAll ("[, =:\"]+ "," "); ObjectName pattern = new ObjectName ("com.oracle.jdbc:type=diagnosability,name=" + loader); ObjectName diag = ((objectname[]) ( Mbs.querynames (pattern, null). ToArray (new objectname[0])) [0];mbs.setattribute (Diag, New Attribute ("loggingenabled ", true)); System.out.println ("loggingenabled =" + Mbs.getattribute (diag, "loggingenabled"));
assume that Oracle 10g is used to run directly:
/**oracle 10g Boot Method */oracle.jdbc.driver.oraclelog.settrace (TRUE); Enable Loggingoracle.jdbc.driver.OracleLog.setTrace (FALSE); Disable logging
3. Configure the log fileJDBC is using the the Logger object in the Java.util.logging package prints the log.
/** Configuration log file */handler fh = new Filehandler ("./oracle_jdbc_log.log"); Fh.setlevel (Level.all); Fh.setformatter (new Simpleformatter ()); Logger.getlogger (""). AddHandler (FH); Logger.getlogger (""). SetLevel (Level.all);
4. Here is a complete demo for everyone
import Javax.management.attribute;import Javax.management.attributenotfoundexception;import Javax.management.instancenotfoundexception;import Javax.management.invalidattributevalueexception;import Javax.management.mbeanexception;import Javax.management.mbeanserver;import Javax.management.malformedobjectnameexception;import Javax.management.objectname;import javax.management.ReflectionException; 
public void Run () throws SQLException {Drivermanager.registerdriver (New oracledriver ());// The logenableoraclelogging () must be opened after loading the oracledriver; Connection conn = null; Statement stmt = null; ResultSet RSet = null;try {conn = getconnection (); System.out.println ("Connection retrieved.."); stmt = Conn.createstatement (), RSet = Stmt.executequery ("Select empno from emp"), while (Rset.next ()) {System.out.println (Rset.getint (1));}} catch (SQLException Sqle) {sqle.printstacktrace ();} finally {if (RSet! = null) {Rset.close ();} if (stmt! = null) {Stmt.close ();} IF (conn! = null) {Conn.close ();}}} public static void Enableoraclelogging () {try {/** config log file */handler fh = new Filehandler ("./oracle_jdbc_log.log"); Fh.set Level (Level.all); Fh.setformatter (new Simpleformatter ()); Logger.getlogger (""). AddHandler (FH); Logger.getlogger (""). SetLevel (level.all)/** Oracle 11g boot method */mbeanserver mbs = Managementfactory.getplatformmbeanserver (); String loader = Thread.CurrentThread (). Getcontextclassloader (). toString (). ReplaceAll ("[, =:\ "]+", "" "), ObjectName pattern = new ObjectName (" com.oracle.jdbc:type=diagnosability,name= "+ loader); ObjectName Diag = ((objectname[]) (Mbs.querynames (pattern, null). ToArray (new objectname[0])) [0];mbs.setattribute (Diag, new Attribute ("loggingenabled", true)); System.out.println ("loggingenabled =" + Mbs.getattribute (diag, "loggingenabled"));/** Oracle 10g boot Method */// Oracle.jdbc.driver.OracleLog.setTrace (TRUE); Enable logging//Oracle.jdbc.driver.OracleLog.setTrace (false); Disable logging} catch (Exception e) {e.printstacktrace ();}}


How to start the JDBC Debug mode and print the JDBC diagnostic log

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.