From 26f3817b48db4765e4bcb127d95f2a1d0ce674e3 Mon Sep 17 00:00:00 2001
From: lzh <3394874942@qq.com>
Date: 星期二, 29 十月 2024 15:18:58 +0800
Subject: [PATCH] Merge branch 'master' of ssh://115.28.86.8:29418/~admin/昆仑_1025

---
 Server/王琨元/document/单例模式封装.txt |   89 +++++++++++++++++++-------------------------
 1 files changed, 39 insertions(+), 50 deletions(-)

diff --git "a/Server/\347\216\213\347\220\250\345\205\203/document/\345\215\225\344\276\213\346\250\241\345\274\217\345\260\201\350\243\205.txt" "b/Server/\347\216\213\347\220\250\345\205\203/document/\345\215\225\344\276\213\346\250\241\345\274\217\345\260\201\350\243\205.txt"
index 2532b6c..c07f1df 100644
--- "a/Server/\347\216\213\347\220\250\345\205\203/document/\345\215\225\344\276\213\346\250\241\345\274\217\345\260\201\350\243\205.txt"
+++ "b/Server/\347\216\213\347\220\250\345\205\203/document/\345\215\225\344\276\213\346\250\241\345\274\217\345\260\201\350\243\205.txt"
@@ -1,62 +1,51 @@
 #include <iostream>
+#include <memory>
 #include <mutex>
-#include <fstream>
 #include <string>
-#include <ctime>
-using namespace std;
-class A {
+#include <mysql_driver.h>
+#include <mysql_connection.h>
+#include <cppconn/statement.h>
+#include <cppconn/resultset.h>
+
+class DatabaseOperator {
 private:
-    ofstream logFile;
-    A() {
-        logFile.open("123.txt", ios::app);
+    // 绉佹湁鏋勯�犲嚱鏁�
+    DatabaseOperator() {
+        try {
+            driver = sql::mysql::get_mysql_driver_instance();
+            connection = driver->connect("tcp://127.0.0.1:3306", "username", "password");
+            connection->setSchema("your_database");
+        } catch (sql::SQLException &e) {
+            std::cerr << "鏁版嵁搴撹繛鎺ラ敊璇�: " << e.what() << std::endl;
+        }
     }
-    ~A() {
-        logFile.close();
-    }
-    A(const A &t){}
-    A& operator=(const A &t){}
-    static A* volatile s_obj;
-    static mutex g_mutex;
+
+    static DatabaseOperator* instance;
+    static std::mutex mutex;
+    sql::Driver* driver;
+    std::unique_ptr<sql::Connection> connection;
+
 public:
-    static A* getInstance() {
-        if (s_obj == nullptr) {
-            lock_guard<mutex> guard(g_mutex);
-            if (s_obj == nullptr) {
-                s_obj = new A;
-            }
+    // 鑾峰彇鍗曚緥瀹炰緥
+    static DatabaseOperator* getInstance() {
+        std::lock_guard<std::mutex> lock(mutex);
+        if (instance == nullptr) {
+            instance = new DatabaseOperator();
         }
-        return s_obj;
+        return instance;
     }
-    void write(const string& level, const string& description, const string& time) {
-        logFile << "[" << level << "] " << "[" << time << "] " << description << endl;
-    }
-    void release() {
-        if (s_obj) {
-            delete s_obj;
-            s_obj = nullptr;
+
+    // 鎵ц鏌ヨ鎿嶄綔锛堢ず渚嬶級
+    sql::ResultSet* query(const std::string& sql) {
+        try {
+            std::unique_ptr<sql::Statement> stmt(connection->createStatement());
+            return stmt->executeQuery(sql);
+        } catch (sql::SQLException &e) {
+            std::cerr << "鏌ヨ閿欒: " << e.what() << std::endl;
         }
+        return nullptr;
     }
 };
 
-A* volatile A::s_obj = nullptr;
-mutex A::g_mutex;
-
-int main() {
-    A* a1 = A::getInstance();
-    A* a2 = A::getInstance();
-    if (a1 == a2) {
-        cout << "鍗曚緥鎴愬姛" << endl;
-    }
-    else {
-        cout << "鍗曚緥澶辫触" << endl;
-    }
-    time_t now = time(nullptr);
-    char buffer[80];
-    struct tm timeinfo;
-    localtime_s(&timeinfo, &now);
-    strftime(buffer, 80, "%Y - %m - %d %H:%M:%S", &timeinfo);
-    string timeStr(buffer);
-    a1->write("1", "鏃ュ織", timeStr);
-    a1->release();
-    return 0;
-}
\ No newline at end of file
+std::mutex DatabaseOperator::mutex;
+DatabaseOperator* DatabaseOperator::instance = nullptr;
\ No newline at end of file

--
Gitblit v1.8.0