之前有写java通过c3p0连接池与数据库进行连接。但是之前写的和普通的连接没啥子区别的样子,完全没有起到池的作用。我们知道,池应该可以理解为一种对数据库的一种直接映射,但是又有不同。池的作用,其实就是一些小的数据库的集群。我们所要的,都在这个池子里。从而避免了对数据库的直接操作,也就是这个池就是我们的数据库。
因为原来写的没有起到池的作用,只起到了一个中间者的作用,所以这次再来一次好了。自定义一个数据库连接池,大家可以自行修改的。
package com.sun.db;
import java.sql.*;
/**
* @author SunY
* 自定义的数据库连接池
* */
public class JDBCUtil {
/**
* The load driver
* */
public static void loadDriver() throws ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
}
/**
* @param
* @return connection
* */
public static Connection getconnection() throws SQLException {
try {
loadDriver();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
}
/**
* @param connection
* @param resultset
* @param statement
* */
public static void release(Connection connection , Statement statement , ResultSet resultset) throws SQLException {
if(null != connection) {
connection.close();
connection = null ;
}
if(null != statement) {
statement.close();
statement = null ;
}
if(null != resultset) {
resultset.close();
resultset = null ;
}
}
/**
* @param connection
* @param statement
* */
public static void release(Connection connection , Statement statement ) throws SQLException {
if(null != connection) {
connection.close();
connection = null ;
}
if(null != statement) {
statement.close();
statement = null ;
}
}
}
package com.sun.db;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.sql.DataSource;
/**
* @author SunY
* */
public class DBManager implements DataSource{
List<Connection> conns = new ArrayList<>();
private final static long INIT_SIZE = 5 ;// 数据库连接池的初始大小
public DBManager() {
super();
for (int i = 0; i < INIT_SIZE; i++) {
// 得到一个连接
try {
Connection connection = JDBCUtil.getconnection();
conns.add(connection);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public Connection getConnection() throws SQLException {
// 当连接池满了或者为空时
if(conns.size() == 0 || conns.isEmpty()) {
for(int i = 0 ; i < 5 ; i ++ ) {
conns.add(JDBCUtil.getconnection());
}
}
return conns.remove(0);
}
/**
*
* 将连接返回连接池
* @param conn
* */
public void addBack(Connection conn) {
conns.add(conn);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
@Override
public void setLogWriter(PrintWriter arg0) throws SQLException {
}
@Override
public void setLoginTimeout(int arg0) throws SQLException {
}
@Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
return false;
}
@Override
public <T> T unwrap(Class<T> arg0) throws SQLException {
return null;
}
}
package com.sun.operate;
import java.sql.*;
import com.sun.db.DBManager;
/**
* @author SunY
* */
public class Test {
public static void main(String[] args) {
DBManager db = new DBManager();
PreparedStatement statement = null ;
Connection connection =null ;
try {
connection = db.getConnection();
String sql = "insert into user(id,username,password) values (?,?,?)";
statement = connection.prepareStatement(sql);
statement.setInt(1,5);
statement.setString(2, "sunyang");
statement.setString(3, "sun18895378485");
int rs = statement.executeUpdate();
if(rs!=0) {
System.out.println("insert successful");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(null != connection) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = null ;
}
db.addBack(connection);
if(null != statement) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
statement = null ;
}
}
}
}
到此便结束了。我还是相信虽然解释很少,还是看得懂的。