现状
- 加载DriverManager的时候,logWriter==null
- 首先执行静态代码块
- 静态代码块会打印日志
#
问题
方案一
DriverManager.setLogWriter(new PrintWriter(System.out));
方案二
package com.zby.driver;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;
public class LogDriver implements Driver {
static {
DriverManager.setLogWriter(new PrintWriter(System.out));
}
@Override
public Connection connect(String url, Properties info) throws SQLException {
return null;
}
@Override
public boolean acceptsURL(String url) throws SQLException {
return false;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return null;
}
@Override
public int getMajorVersion() {
return 0;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public boolean jdbcCompliant() {
return false;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
- 创建META-INF/services/java.sql.Driver文件,写入内容com.zby.driver.LogDriver