mysql依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
配置文件 db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.232.211:3306/school
user = root
password=ok
public class FinalFields {
public static final String MYSQL_DRIVER_STR="driver";
public static final String MYSQL_USER_STR="user";
public static final String MYSQL_URL_STR="url";
public static final String MYSQL_PWD_STR="password";
}
加载配置文件
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropUtils {
public static Properties getProperties(String profileName) throws IOException {
ClassLoader classLoader = PropUtils.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream(profileName);
Properties props = new Properties();
props.load(is);
return props;
}
}
建立Connection对象
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class Mysql {
private static String driver;
private static String url;
private static String user;
private static String password;
static {
Properties props = null;
try {
props = PropUtils.getProperties("db.properties");
} catch (IOException e) {
e.printStackTrace();
}
driver=props.getProperty(FinalFields.MYSQL_DRIVER_STR);
url=props.getProperty(FinalFields.MYSQL_URL_STR);
user=props.getProperty(FinalFields.MYSQL_USER_STR);
password=props.getProperty(FinalFields.MYSQL_PWD_STR);
}
public static Connection getConnection(){
System.out.println(driver);
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection conn, PreparedStatement psmt, ResultSet rs){
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (psmt!=null) {
try {
psmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection connection = Mysql.getConnection();
System.out.println(connection);
Mysql.close(connection,null,null);
}
}
package nj.zb.kb11;
import java.sql.Connection;
import java.util.LinkedList;
public class MysqlConnectionPool {
private Integer initNum = 5;
private Integer increase = 5;
private Integer min = 3;
private Integer max = 20;
private Byte b = 1;
private boolean xiaoguogo = false;
private LinkedList<Connection> pool = new LinkedList<>();
private void produceConnection(Integer num) {
for (int i = 0; i < num; i++) {
if(!checkPoolMaxNum()){
xiaoguogo = false;
pool.add(JDBCUtils.getConnection());
}
}
}
private boolean checkPoolMaxNum() {
if (pool.size() > max) {
return true;
} else {
return false;
}
}
private MysqlConnectionPool() {
produceConnection(initNum);
}
public Integer count() {
return pool.size();
}
public Connection getConnection() {
synchronized (b) {
while (pool.size() == 0) {
System.out.println("warning, 连接池没有连接。。。。。。");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (pool.size() <= min && !xiaoguogo) {
xiaoguogo = true;
new Thread(new Runnable() {
@Override
public void run() {
produceConnection(increase);
}
}).start();
}
return pool.pop();
}
}
public void close(Connection conn) {
synchronized (b) {
if (conn == null) {
return;
} else {
if (checkPoolMaxNum()) {
JDBCUtils.close(conn);
} else {
pool.add(conn);
}
}
}
}
private static MysqlConnectionPool poolInstance2 = new MysqlConnectionPool();
public static MysqlConnectionPool getPoolInstance() {
return poolInstance2;
}
}