日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/15 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;

}

}

總結

以上是生活随笔為你收集整理的java mysql 线程安全_java连接mysql的线程安全问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。