一个类被加载的时候,有那些被加进来了,具体到我这个程序,只讨论这个类被加载时的情况:
先是初始化代码块,然后是静态成员,是吗?package pro.jdbc;import java.sql.*;public class JdbcUtils {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "zhangwei"; private JdbcUtils() {
} static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
} public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
} public static void free(ResultSet rs, Statement stat, Connection conn) {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stat != null)
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
先是初始化代码块,然后是静态成员,是吗?package pro.jdbc;import java.sql.*;public class JdbcUtils {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "zhangwei"; private JdbcUtils() {
} static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
} public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
} public static void free(ResultSet rs, Statement stat, Connection conn) {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stat != null)
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
再说static语句块中 Class.forName(driver);必须等driver初始化后执行才有意义,所以才这么安排。
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;public class MyConnectionPool {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "**";
private static LinkedList<Connection> connectionPool = new LinkedList<Connection>(); public MyConnectionPool() {
} static {
try {
Class.forName(driver);
for (int i = 0; i < 5; i++) {
connectionPool.addLast(create());
}
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
} private static Connection create() throws SQLException {
return DriverManager.getConnection(url, user, password);
} public Connection getConnection() {
return connectionPool.removeFirst();
} public void free(ResultSet rs, Statement stat, Connection conn) {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stat != null)
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
connectionPool.addLast(conn);
}
}
}
}package pro.jdbc;import java.sql.Connection;
import java.sql.SQLException;public class ConnectionTest {
public static void main(String[] args) throws SQLException {
Connection conn = null;
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
MyConnectionPool cPool = new MyConnectionPool();
conn = cPool.getConnection();
System.out.println(conn);
cPool.free(null, null, conn);
}
long end = System.currentTimeMillis();
//运行出来为2610秒,下面,改造工具类,引入连接池的概念
System.out.println("the time of create connection is :" + (end - start));
}}
你要将代码:
for (int i = 0; i < 5; i++) {
connectionPool.addLast(create());
}从static代码块中移走,移到 public MyConnectionPool() 构造方法中。即构造方法中应为:try{
for (int i = 0; i < 5; i++) {
connectionPool.addLast(create());
}
}catch (Exception e) {
throw new ExceptionInInitializerError(e);
}原因是:
static代码块只执行一次,而构造器MyConnectionPool() 每new一次都执行。而你的代码代码中:Class.forName()只要执行一次,而那个for()循环是要每new一次都要执行的。
public void free(ResultSet rs, Statement stat, Connection conn) {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (stat != null)
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null)
connectionPool.addLast(conn);
}