稍微修改了下,可能会好一些,建议还是听上面那哥们的,使用成熟的数据库连接池,没必要重复造轮子
使用单例,保证数据库连接的唯一性
修改synchronized关键字的用法,提高效率
增加volatile关键字,提高稳定性
package com.singleton;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 功能:
*
* 完整路径: com.singleton.MySQLUtil
* 创建日期: 2017年6月15日 上午10:42:49
*
* @author pfyangf
* @version 1.0
*/
class MySQLUtil {
private MySQLUtil(){}
private static volatile Connection connection = null;
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://192.168.31.103:3306/";
private static final String character = "?useUnicode=true&characterEncoding=utf8";
private static final String ssl = "&useSSL=false";
private static final String user = "axtest";
private static final String password = "axtest123";
private static Statement statement = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
public static void main(String[] args) {
/*Connection newConnection;
try {
newConnection = MySQLUtil.connectToDB("xxx");
System.out.println(newConnection.isClosed());
} catch (Exception e) {
//TODO 异常处理
e.printStackTrace();
}*/
try {
List> data = MySQLUtil.readData("xxx", "select now() from dual");
System.out.println(data.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
boolean TestConnection(String db) {
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);
if (!connection.isClosed()) {
CloseConnection();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 功能:获取DB连接
*
* @Author:pfyangf , 2017年6月15日
* @param db
* @return
* @throws Exception Connection
**/
public static Connection connectToDB(String db) throws Exception {
if(null == connection){
synchronized (MySQLUtil.class) {
if(null == connection){
Class.forName(driver);
connection = DriverManager.getConnection(url + db + character + ssl, user, password);
statement = connection.createStatement();
}
}
}
return connection;
}
private static void CloseConnection() {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void ModifyData(String db, String data) throws Exception {
connectToDB(db);
try {
statement.execute(data);
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseConnection();
}
}
public static List> readData(String db, String sql) throws Exception {
List> list = new ArrayList<>();
int count;
connectToDB(db);
try {
rs = statement.executeQuery(sql);
ResultSetMetaData rsmd;
rsmd = rs.getMetaData();
count = rsmd.getColumnCount();
while (rs.next()) {
Map map = null;
for (int i = 1; i <= count; i++) {
map = new HashMap<>();
map.put(rsmd.getColumnLabel(i), rs.getString(i));
list.add(map);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseConnection();
}
return list;
}
}