From aae2a9ba3a33136e4106e36e47fbd5b5c31c4fd9 Mon Sep 17 00:00:00 2001
From: 18516761980 <4761516tqsxp>
Date: 星期四, 16 十二月 2021 15:16:24 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/common/service/erp/ErpSqlServer.java |  115 ++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 92 insertions(+), 23 deletions(-)

diff --git a/src/main/java/com/zy/common/service/erp/ErpSqlServer.java b/src/main/java/com/zy/common/service/erp/ErpSqlServer.java
index f419226..481444b 100644
--- a/src/main/java/com/zy/common/service/erp/ErpSqlServer.java
+++ b/src/main/java/com/zy/common/service/erp/ErpSqlServer.java
@@ -2,6 +2,7 @@
 
 import com.core.common.Cools;
 import com.zy.common.properties.ErpDbProperties;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -19,11 +20,12 @@
  * 绠�鍗曟寔涔呭眰妗嗘灦
  * Created by vincent on 2020/11/26
  */
+@Slf4j
 @Service
 public class ErpSqlServer {
 
     // 鏁版嵁搴撹繛鎺�
-    private Connection conn;
+//    private Connection conn;
     // 鍒涘缓棰勭紪璇戣鍙ュ璞★紝涓�鑸兘鏄敤杩欎釜鑰屼笉鐢⊿tatement
     private PreparedStatement pstm = null;
     // 鍒涘缓涓�涓粨鏋滈泦瀵硅薄
@@ -67,8 +69,9 @@
 
 
     private List<Map<String, Object>> executeQuery(String sql) {
+        Connection conn = null;
         try {
-            Connection conn = getConn();
+            conn = getConn();
             pstm = conn.prepareStatement(sql);
             rs = pstm.executeQuery();
 //            List<Map<String, Object>> maps = convertList(rs);
@@ -78,19 +81,73 @@
             return null;
         } finally {
             release();
+            if (conn != null) {
+                try {
+                    conn.close();
+                    conn = null;
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }
 
-    private int executeUpdate(String sql) {
+
+    public Number executeQueryCount(String sql, String column) {
+        Number value = 0;
+        Connection conn = null;
         try {
-            Connection conn = getConn();
+            conn = getConn();
             pstm = conn.prepareStatement(sql);
-            return pstm.executeUpdate();
+            rs = pstm.executeQuery();
+//            ResultSetMetaData metaData = rs.getMetaData();
+            while (rs.next()) {
+                value = (Number) rs.getInt(column);
+            }
+            return value;
         } catch (Exception e) {
             e.printStackTrace();
             return 0;
         } finally {
             release();
+            if (conn != null) {
+                try {
+                    conn.close();
+                    conn = null;
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    private int executeUpdate(String sql) {
+        Connection conn = null;
+        try {
+            conn = getConn();
+            if(null != conn)
+            {
+                pstm = conn.prepareStatement(sql);
+            } else {
+                log.error("鏇存柊ERP涓棿琛ㄥけ璐�===>>鏁版嵁搴撹繛鎺onn涓虹┖");
+                log.error("鏇存柊ERP涓棿琛ㄥけ璐�===>>" + sql);
+            }
+            return pstm.executeUpdate();
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("鏇存柊ERP涓棿琛ㄥけ璐�===>>" + sql);
+            log.error("鏇存柊ERP涓棿琛ㄥけ璐�===>>" + e);
+            return 0;
+        } finally {
+            release();
+            if (conn != null) {
+                try {
+                    conn.close();
+                    conn = null;
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }
 
@@ -108,17 +165,29 @@
         return list;
     }
 
-    private Connection getConn() {
-        if (null == this.conn) {
-            try {
-                Class.forName(erpDbProperties.getDriver_class_name()).newInstance();
-                this.conn = DriverManager.getConnection(erpDbProperties.getUr(), erpDbProperties.getUsername(), erpDbProperties.getPassword());
-            } catch (Exception e) {
-                e.printStackTrace();
-                throw new RuntimeException("鑾峰彇ERP鏁版嵁搴撹繛鎺ュけ璐�");
-            }
+    //synchronized
+    public Connection getConn() throws SQLException {
+        Connection conn = null;
+        try {
+            Class.forName(erpDbProperties.getDriver_class_name()).newInstance();
+            conn = DriverManager.getConnection(erpDbProperties.getUr(), erpDbProperties.getUsername(), erpDbProperties.getPassword());
+        } catch (Exception e) {
+            log.error("鑾峰彇ERP鏁版嵁搴撹繛鎺ュけ璐�");
+            e.printStackTrace();
+            throw new RuntimeException("鑾峰彇ERP鏁版嵁搴撹繛鎺ュけ璐�");
         }
-        return this.conn;
+        return conn;
+//        if (null == this.conn || this.conn.isClosed()) {
+//            try {
+//                Class.forName(erpDbProperties.getDriver_class_name()).newInstance();
+//                this.conn = DriverManager.getConnection(erpDbProperties.getUr(), erpDbProperties.getUsername(), erpDbProperties.getPassword());
+//            } catch (Exception e) {
+//                log.error("鑾峰彇ERP鏁版嵁搴撹繛鎺ュけ璐�");
+//                e.printStackTrace();
+//                throw new RuntimeException("鑾峰彇ERP鏁版嵁搴撹繛鎺ュけ璐�");
+//            }
+//        }
+//        return this.conn;
     }
 
     private void release() {
@@ -136,14 +205,14 @@
                 e.printStackTrace();
             }
         }
-        if (conn != null) {
-            try {
-                conn.close();
-                conn = null;
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
-        }
+//        if (conn != null) {
+//            try {
+//                conn.close();
+//                conn = null;
+//            } catch (SQLException e) {
+//                e.printStackTrace();
+//            }
+//        }
     }
 
     /**

--
Gitblit v1.9.1