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++) { 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 al_sql_strs) { //System.out.println("释放之前************"); //DBUtil.getConnections(); boolean exe_res = true; try { mysql_con.setAutoCommit(false); for(int n=0; n