62,614
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}
}
}
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);
}
try{
for (int i = 0; i < 5; i++) {
connectionPool.addLast(create());
}
}catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
package pro.jdbc;
import java.sql.Connection;
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));
}
}