但行好事
莫论前程❤

Eclipse打印存储过程里的日志

Oracle存储过程中添加:

Dbms_Output.Put_Line("开始打印日志");

Eclipse端的设置在debug页面添加启动参数

test 
DbmsOutput

调用方法

/**
     * 
     * 调用存储过程统一用这个方法
     */
    public Object executePRC(String callString, CallableStatementCallback callback) {

        // 解决:建行代码(fortify)扫描的bug
        String dbmsOutputStr = "true";
        if (dbmsOutputStr != null && dbmsOutputStr.equals("true")) {
            Connection conn = org.springframework.jdbc.datasource.DataSourceUtils.getConnection(SessionContext
                    .getSessionContext().getDataSource());
            DbmsOutput dbmsOutput = null;
            try {
                dbmsOutput = new DbmsOutput(conn);
                dbmsOutput.enable(1000000);
                dbmsOutput.show();
                return getJdbcTemplate().execute(callString, callback);

            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);// To change body of catch
            } finally {
                try {
                    dbmsOutput.show();
                    dbmsOutput.close();
                } catch (SQLException e) {
                    e.printStackTrace(); // To change body of catch statement
                    throw new RuntimeException(e);
                }
            }
        } else {
            return getJdbcTemplate().execute(callString, callback);
        }
    }

DbmsOutput.java

package nc.bs.foundation.util;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2007-11-9
 * Time: 12:10:01
 * To change this template use File | Settings | File Templates.
 *
 *      打印oracle存储过程的输出类
 *      使用方法:
 *
 *      Connection conn = DataSourceUtils.getConnection(SessionContext.getSessionContext().getDataSource());
        DbmsOutput dbmsOutput = null;
        try {
            dbmsOutput = new DbmsOutput(conn);
            dbmsOutput.enable( 1000000 );

            getJdbcTemplate().execute("{call PRC_GEN_VOUCHERANDFLOWS(?,?,?,?,?)}", callback);

        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        finally{
            try {
                dbmsOutput.show();
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
 **/
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DbmsOutput
{
    private CallableStatement enable_stmt;
    private CallableStatement disable_stmt;
    private CallableStatement show_stmt;

    public DbmsOutput( Connection conn ) throws SQLException
    {
        enable_stmt = conn.prepareCall( "begin dbms_output.enable(:1); end;" );
        disable_stmt = conn.prepareCall( "begin dbms_output.disable; end;" );

        show_stmt = conn.prepareCall(
            "declare " +
                " l_line varchar2(255); " +
                " l_done number; " +
                " l_buffer long; " +
                "begin " +
                " loop " +
                " exit when length(l_buffer)+255 > :maxbytes OR l_done = 1; " +
                " dbms_output.get_line( l_line, l_done ); " +
                " l_buffer := l_buffer || l_line || chr(10); " +
                " end loop; " +
                " :done := l_done; " +
                " :buffer := l_buffer; " +
                "end;" );
    }

    public void enable( int size ) throws SQLException
    {
        enable_stmt.setInt( 1, size );
        enable_stmt.executeUpdate();
    }

    public void disable() throws SQLException
    {
        disable_stmt.executeUpdate();
    }

    public void show() throws SQLException
    {
        int done = 0;

        show_stmt.registerOutParameter( 2, java.sql.Types.INTEGER );
        show_stmt.registerOutParameter( 3, java.sql.Types.VARCHAR );

        for(;;)
        {
            show_stmt.setInt( 1, 32000 );
            show_stmt.executeUpdate();
//            System.out.print( show_stmt.getString(3) );
            if ( (done = show_stmt.getInt(2)) == 1 ) break;
        }
    }

    public void close() throws SQLException
    {
        enable_stmt.close();
        disable_stmt.close();
        show_stmt.close();
    }
}



class test {

    public static void main (String args [])
        throws SQLException
    {
        DriverManager.registerDriver
            (new oracle.jdbc.driver.OracleDriver());

        Connection conn = DriverManager.getConnection
            ("jdbc:oracle:thin:@dbserver:1521:ora8i",
                "scott", "tiger");
        conn.setAutoCommit (false);

        Statement stmt = conn.createStatement();

        DbmsOutput dbmsOutput = new DbmsOutput( conn );

        dbmsOutput.enable( 1000000 );

        stmt.execute
            ( "begin emp_report; end;" );
        stmt.close();

        dbmsOutput.show();

        dbmsOutput.close();
        conn.close();
    }

}
赞(0) 打赏
未经允许不得转载:刘鹏博客 » Eclipse打印存储过程里的日志
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏