了解DriverManager

它其实就是管理我们的数据库驱动的:
public static synchronized void registerDriver(java.sql.Driver driver,
DriverAction da)
throws SQLException {

/* Register the driver if it has not already been added to our list */
if(driver != null) {
    registeredDrivers.addIfAbsent(new DriverInfo(driver, da));    //在刚启动时,mysql实现的驱动会被加载,我们可以断点调试一下。
} else {
    // This is for compatibility with the original DriverManager
    throw new NullPointerException();
}

println("registerDriver: " + driver);

}
我们可以通过调用getConnection()来进行数据库的链接:
@CallerSensitive
public static Connection getConnection(String url,
String user, String password) throws SQLException {
java.util.Properties info = new java.util.Properties();

if (user != null) {
    info.put("user", user);
}
if (password != null) {
    info.put("password", password);
}

return (getConnection(url, info, Reflection.getCallerClass()));   //内部有实现

}
我们可以手动为驱动管理器添加一个日志打印:

      static {
DriverManager.setLogWriter(new PrintWriter(System.out));   //这里直接设定为控制台输出

}
现在我们执行的数据库操作日志会在控制台实时打印。

posted @ 2024-11-14 23:46  雨花阁  阅读(3)  评论(0编辑  收藏  举报