• 怎样启动JDBC Debug模式,打印JDBC诊断日志


    1.下载Debug版本号jar包
         首先要下载一个Debug版本号的JDBC jar包,Debug版本号的jar包命名形式为jdbcX_g.jar(例如以下图所看到的)。如Oracle11g的Debug版本号的jar包为jdbc6_g.jar。

    点击这里下载:http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html 


    2.启动Logging模式
         第一种方式:设置执行环境变量java -Doracle.jdbc.Trace=true ...(elipse中run->run configurations->arguments一栏)
         另外一种方式:在程序中控制(建议採用这样的方式)
    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,直接运行:
    /**oracle 10g 启动方法*/
    			oracle.jdbc.driver.OracleLog.setTrace(true); // enable logging
    			oracle.jdbc.driver.OracleLog.setTrace(false); // disable logging
    3.配置log文件
    jdbc使用的是java.util.logging包中的Logger对象打印log。
    	/**配置log文件*/
    			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.以下给大家一个完毕的Demo
    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());
    		//需在载入OracleDriver之后开启log
    		enableOracleLogging();
    		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 {
    			/** 配置log文件 */
    			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);
    			/** oracle 11g 启动方法 */
    			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 启动方法 */
    			// oracle.jdbc.driver.OracleLog.setTrace(true); // enable logging
    			// oracle.jdbc.driver.OracleLog.setTrace(false); // disable logging
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    


  • 相关阅读:
    JS 改变鼠标样式
    jquery 实践操作:iframe 相关操作
    jquery 实践操作:div 动态嵌套(追加) div
    jquery 实践操作:div 动态嵌套页面
    jquery 实践操作:attr()方法
    jquery 实践操作:load()方法
    nodeJS(2)深了解: nodeJS 项目架构详解(app.js + Express + Http)
    photoshop 安装
    Github 使用记录
    PhpStorm 10.0.1安装(win7-64位)
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7258355.html
Copyright © 2020-2023  润新知