whycxzp
2025-04-10 8f43ce26799c7c1b490507061fcc3c7d1b9e84e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.whyc.service;
 
import com.whyc.mapper.CallBack;
import com.whyc.util.ThreadLocalUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class JdbcSqlExecuteService {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
 
    //    自定义执行SQL
    public JSONArray executeQuery(String sql){
        PreparedStatement ps = null;
        ResultSet rs = null;
        SqlSession sqlSession = openSession();
        JSONArray jsonArray = new JSONArray();
        try {
            ps=sqlSession.getConnection().prepareStatement(sql);
            rs=ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int colCnt = metaData.getColumnCount();
            while (rs.next()) {
                JSONObject jsonObject = new JSONObject();
                for(int i=1;i<=colCnt;i++){
                    String property=metaData.getColumnName(i);
                    Object obj=rs.getObject(i);
                    if(obj instanceof java.util.Date ){
                        //jsonObject.put(property, ActionUtil.sdf.format(obj));
                        jsonObject.put(property, ThreadLocalUtil.format((java.util.Date) obj,1));
                    }else{
                        jsonObject.put(property,obj);
                    }
                }
                JSONObject.toBean(jsonObject,Object.class);
                jsonArray.add(jsonObject);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeSession(rs,ps,sqlSession);
        }
        return jsonArray;
    }
 
    //    自定义执行SQL
    public List executeQuery_call(String sql, CallBack call){
        PreparedStatement ps = null;
        ResultSet rs = null;
        SqlSession sqlSession = openSession();
        try {
            ps=sqlSession.getConnection().prepareStatement(sql);
            rs=ps.executeQuery();
            return call.getResults(rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeSession(rs,ps,sqlSession);
        }
        return null;
    }
 
    /**
     * 封装所有更新的操作(添加,删除,修改)
     * @param sql
     * @param values
     * @return
     */
    public  int executeUpdate(String sql, Object[] values){
        PreparedStatement ps = null;
        ResultSet rs = null;
        SqlSession sqlSession = openSession();
        int flag=0;
        try {
            ps = sqlSession.getConnection().prepareStatement(sql);
            if(values != null){
                for (int i = 0; i < values.length; i++) {
                    ps.setObject(i+1, values[i]);
                }
            }
            flag =  ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeSession(rs,ps,sqlSession);
        }
        return flag;
    }
    //执行sql语句
    public  boolean execute(String sql){
        PreparedStatement ps = null;
        SqlSession sqlSession = openSession();
        boolean rest = true;
        try{
                ps = sqlSession.getConnection().prepareStatement(sql);
                ps.execute();
 
        }catch(SQLException ex) {
            ex.printStackTrace();
            rest = false;
        }finally {
            closeSession(null,ps,sqlSession);
        }
 
        return rest;
    }
    //    开启链接
    private SqlSession openSession() {
        SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
        return sqlSessionFactory.openSession();
    }
 
    //    关闭链接
    private void closeSession(ResultSet rs,PreparedStatement ps,SqlSession sqlSession) {
        try {
            if(rs!=null){
                rs.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(sqlSession!=null){
                sqlSession.close();
            }
 
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
 
    public  boolean makeManualCommit( ArrayList<String> al_sql_strs){
        PreparedStatement ps = null;
        SqlSession sqlSession = openSession();
        Connection mysql_con=sqlSession.getConnection();
        boolean exe_res = true;
        try {
            mysql_con.setAutoCommit(false);
 
            for(int n=0; n<al_sql_strs.size(); n++) {
                if(true == exe_res) {
                    exe_res = executeCreateTableNoclose(mysql_con,al_sql_strs.get(n));
                } else {
                    break;
                }
            }
            if(true == exe_res) {
                mysql_con.commit();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            exe_res = false;
        } finally {
            try {
                if(false == exe_res) {
                    mysql_con.rollback();
                }
                mysql_con.setAutoCommit(true);
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }finally {
                closeSession(null,ps,sqlSession);
            }
        }
        return exe_res;
    }
 
    //执行sql语句with no colse
    public static boolean executeCreateTableNoclose(Connection mysql_con,String sql_str)
    {
        PreparedStatement ps=null;
 
        boolean rest = true;
        try
        {
            ps = mysql_con.prepareStatement(sql_str);
            ps.setQueryTimeout(600);
            ps.execute();
        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
            rest = false;
 
        }finally {
            try {
                ps.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return rest;
    }
}