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

Source: Internet
Author: User

1. Download the Debug version jar packageFirst to download a debug version of the JDBC Jar package, the debug version of the jar package is named Jdbcx_g.jar (as shown), such as ORACLE11G's debug version of the jar package is Jdbc6_g.jar. Click here to download: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
2. Start logging modeThe first way: Set the Run environment variablejava-doracle.jdbc.trace=true ... (Elipse in Run->run configurations->arguments column)The second 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"));
If you are using Oracle 10g, do this 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's a demo for you to complete
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 ());// Need to open logenableoraclelogging () after loading 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 ();}}


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.