lxw
2020-06-27 8aee9dfec05e1a9333abf03f1bd35dd7cfa9d352
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.fgkj.dao;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.fgkj.db.DBUtil;
import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
 
/**
 * 
 * 数据库操作相关的封装类,封装了所有的增删改以及查询的操作
 * @author Administrator
 *
 */
 
public class DAOHelper {
    public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat sdfwithOut=new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 封装所有更新的操作(添加,删除,修改)
     * @param conn
     * @param sql
     * @param values
     * @return
     */
    public static boolean executeUpdate(Connection conn,String sql,Object[] values){
        //System.out.println("释放之前************");
        //DBUtil.getConnections();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            if(values != null){
                for (int i = 0; i < values.length; i++) {
                    ps.setObject(i+1, values[i]);
                }
            }
            int flag =  ps.executeUpdate();
            if(flag>0){
                return true;
            }
        }catch (MySQLSyntaxErrorException e) {
            e.printStackTrace();
            //System.out.println("权限不足");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(null, ps, conn);
            //System.out.println("释放之后++++++++++++");
            //DBUtil.getConnections();
        }
        
        return false;
    }
    
    /**
     * 根据指定sql语句执行相关查询
     * @param sql        需要执行的sql查询语句
     * @param values    执行sql查询时需要的参数值
     * @param call        回调函数对象:将获取结果的任务以回调的方式交给调用者处理
     * @return
     */
    public static List executeQuery(String sql,Connection conn,Object[] values,CallBack call){
        //System.out.println("释放之前************");
        //DBUtil.getConnections();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setQueryTimeout(600);
            //当查询条件不为空时,设置查询条件对应的值
            if(values != null){
                for (int i = 0; i < values.length; i++) {
                    //System.out.println(values[i]);
                    ps.setObject(i+1, values[i]);
                }
            }
            rs=ps.executeQuery();
            return call.getResults(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(rs, ps, conn);
            //System.out.println("释放之后++++++++++++");
            //DBUtil.getConnections();
        }
        return null;
    }
    
    //对图片的更新操作(添加,删除,修改)
    public static boolean executeUpdatePicture(Connection conn,String sql,List list){
        PreparedStatement ps = null;
        InputStream in = null;
        try {
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < list.size(); i++) {
                Object obj=list.get(i);
                if(obj.getClass().getName().equals("java.io.File")){
                    File file=(File) obj;
                    try {
                        in = new FileInputStream(file);
                        //System.out.println(in);
                        //生成被插入文件的节点流
                        //设置Blob
                         ps.setBlob(i+1, in);
                    } catch (FileNotFoundException e) {
                        conn.rollback();
                        e.printStackTrace();
                    } catch (IOException e) {
                        conn.rollback();
                        e.printStackTrace();
                    }
                }else{
                    ps.setObject(i+1, obj);
                }
            }
            int rs =  ps.executeUpdate();
            if(rs>0){
                conn.commit();
                return true;
            }
        }catch (MySQLSyntaxErrorException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(null, ps, conn);
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
    //对图片的查询操作
    public static List executeQueryPicture(String sql,Connection conn,Object[] values,CallBack call){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setQueryTimeout(600);
            //当查询条件不为空时,设置查询条件对应的值
            if(values != null){
                for (int i = 0; i < values.length; i++) {
                    ps.setObject(i+1, values[i]);
                }
            }
            rs=ps.executeQuery();
            return call.getResults(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(rs, ps, conn);
        }
        return null;
    }
    //用于分页的查询
    public static List executeQueryLimit(String sql,Connection conn,Object[] values,CallBack call){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            //当查询条件不为空时,设置查询条件对应的值
            if(values != null){
                for (int i = 0; i < values.length; i++) {
                    ps.setObject(i+1, values[i]);
                }
            }
            rs=ps.executeQuery();
            return call.getResults(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(rs, ps, null);
        }
        return null;
    }
    //用于修改回滚
    public static boolean executeUpdateLimit(Connection conn,String sql,Object[] values){
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            if(values != null){
                for (int i = 0; i < values.length; i++) {
                    ps.setObject(i+1, values[i]);
                }
            }
            int flag =  ps.executeUpdate();
            conn.commit();
            if(flag>0){
                return true;
            }
        }catch (MySQLSyntaxErrorException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(null, ps, null);
        }
        
        return false;
    }
    /**
     * 
     * @param regex        SimpleDateFormat 的形式
     * @return    根据传入的regex获取的SimpleDateFormat对象
     */
    public static SimpleDateFormat getSdf(String regex){
        if(regex!=null){
            return new SimpleDateFormat(regex);
        }else{
            return  new SimpleDateFormat();
        }
    }
    
    public static double toFixid(Double a,long index){
        StringBuffer ms=new StringBuffer();
        if(a!=null && index>=0){
            if(index==0){
                ms=ms.append("#");
            }else{
                ms=ms.append("#.");
                for(int i=0;i<index;i++){
                    ms.append("0");
                }
                
            }
            //System.out.println(new DecimalFormat(ms.toString()).format(a));
            return Double.parseDouble(new DecimalFormat(ms.toString()).format(a));
        }else{
            return a;
        }
    }
    //执行sql语句
    public static boolean executeCreateTable(Connection mysql_con,String sql_str)
    {  
        PreparedStatement ps=null;
    
        boolean rest = true;
        try
        {
            //System.out.println(sql_str);
            ps = mysql_con.prepareStatement(sql_str);
            ps.setQueryTimeout(600);
            ps.executeUpdate();
        }
        catch(SQLException ex)
        {
            //System.out.println("##########################");
            ex.printStackTrace();
            rest = false;
            DBUtil.close(null, ps, mysql_con);   
            
        }
        return rest;
    }
    //执行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;
            
        }
        return rest;
    }
    
    public static boolean makeManualCommit(Connection mysql_con,ArrayList<String> al_sql_strs) 
    {
        //System.out.println("释放之前************");
        //DBUtil.getConnections();
        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) {
            e.printStackTrace();
            exe_res = false;
        } finally {
            try {
                if(false == exe_res) {
                    mysql_con.rollback();
                }
                mysql_con.setAutoCommit(true);
            } catch (SQLException e1) {
                e1.printStackTrace();
            } finally{
                DBUtil.close(null, null, mysql_con);
                //System.out.println("释放之后++++++++++++");
                //DBUtil.getConnections();
            }
        }
        
        return exe_res;
    }
    
    public static void main(String[] args) {
        //System.out.println(getSdf(null));
        float f=1.2546f;
        System.out.println(toFixid(new Double(f), 9));;
    }
}