java mysql 线程安全_java连接mysql的线程安全问题

稍微修改了下,可能会好一些,建议还是听上面那哥们的,使用成熟的数据库连接池,没必要重复造轮子

使用单例,保证数据库连接的唯一性

修改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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值