}
}
運行起來,你就會看到有這樣的sql語句被執行了:
INSERT INTO jws_log VALUES ('2006-01-18 17:50:22', 'main', 'INFO',
'Wite2Database.main(Write2Database.java:18)', 'test', '')
注意:在賽迪論壇上有一個筆者按照上述類似的方法沒有運行成功,現將他所出現的問題和解決方法轉載。
上述方法是利用傳統的數據庫連接方法,對于數據庫的管理和效率嚴重不足,在現在這個連接池橫行的時代,
為什么我們不能給給Log4j配上連接池,讓Log4j利用數據連接池的連接和數據庫進行通訊。
現查看Log4j的Api,發現JDBCAppender這個類有以下幾段話:WARNING: This version of JDBCAppender is very
likely to be completely replaced in the future. Moreoever, it does not log exceptions. The JDBCAppender
provides for sending log events to a database.
For use as a base class:
Override getConnection() to pass any connection you want. Typically this is used to enable
application wide connection pooling.
Override closeConnection(Connection con) -- if you override getConnection make sure to
implement closeConnection to handle the connection you generated. Typically this would
return the connection to the pool it came from.
Override getLogStatement(LoggingEvent event) to produce specialized or dynamic statements.
The default uses the sql option value.
原來log4j建議我們把其提供的JDBCAppender作為基類來使用,然后Override三個父類的方法:getConnection(),
closeConnection(Connection con)和getLogStatement(LoggingEvent event)。
原來如此,那就寫一個子類JDBCPoolAppender來替代這個JDBCAppender
文章來源于領測軟件測試網 http://www.kjueaiud.com/