代码片段: DBHelper-gistfile1.txt

package com.hotel.util; import java.io.BufferedInputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;...
<pre>package com.hotel.util; import java.io.BufferedInputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import oracle.sql.DATE; public class DBhelp { static{ try { Class.forName(MyProperties.getIntstance().getProperty("driverClass")); } catch (ClassNotFoundException | IOException e) { e.printStackTrace(); } } public Connection getcon(){ Connection con=null; try { con = DriverManager.getConnection(MyProperties.getIntstance().getProperty("url"), MyProperties.getIntstance()); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return con; } public void closeAll(ResultSet rs,Statement stm,Connection con,PreparedStatement ps) throws SQLException{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(stm!=null){ stm.close(); } if(con!=null){ con.close(); } } //增删改 public int doUpdate(String sql,List<Object> params){ int result=-1; Connection con=getcon(); try { PreparedStatement pstm=con.prepareStatement(sql); //设置参数 doParams(pstm,params); //执行sql语句 result=pstm.executeUpdate(); closeAll(null, null, con, pstm); } catch (SQLException e) { e.printStackTrace(); } return result; } private void doParams(PreparedStatement pstm, List<Object> params) { try { if(pstm!=null && params!=null && params.size()>0){ for(int i=0;i<params.size();i++){ Object o=params.get(i); //o是Integer类型 if(o instanceof Integer){ Integer t=Integer.parseInt(o.toString()); pstm.setInt(i+1, t); }else if(o instanceof String){ pstm.setString(i+1,o.toString()); }else if(o instanceof Double){ Double t=Double.parseDouble(o.toString()); pstm.setDouble(i+1, t); }else if(o instanceof Boolean){ Boolean t=Boolean.parseBoolean(o.toString()); pstm.setBoolean(i+1, t); }else if(o instanceof Long){ Long t=Long.parseLong(o.toString()); pstm.setLong(i+1, t); }else if(o instanceof Float){ Float t=Float.parseFloat(o.toString()); pstm.setFloat(i+1, t); }else if(o instanceof Character){ pstm.setString(i+1, o.toString()); }else if(o instanceof Byte){ Byte t=Byte.parseByte(o.toString()); pstm.setByte(i+1, t); }else if(o instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d=sdf.parse(o.toString()); pstm.setDate(i+1, (java.sql.Date) d); }else{//@@ pstm.setBytes(i+1,(byte[]) o); } } } } catch (NumberFormatException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } //查 public List<Map<String,String>> findAll(String sql,List<Object> params){ List<Map<String,String>> list=new ArrayList<Map<String,String>>(); try { Connection con=getcon(); PreparedStatement pstm=con.prepareStatement(sql); doParams(pstm,params); //得到结果集 ResultSet rs=pstm.executeQuery(); //获取所有列名 ResultSetMetaData rsdate=rs.getMetaData(); //存储列名 String[] columnName=new String[rsdate.getColumnCount()]; //获取列名 for(int i=0;i<columnName.length;i++){ columnName[i]=rsdate.getColumnName(i+1); } //根据列名获取值 while(rs.next()){ Map<String,String> map=new HashMap<String,String>(); //根据列名获取数据 for(String cn: columnName){ String value=rs.getString(cn); map.put(cn, value); } list.add(map); } closeAll(rs,null,con,pstm); } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 查询 * @param sql sql语句 * @param params 添加的条件 * @param c 一个标准的javabean对象 * @return */ public <T> List<T> findAll(String sql,List<Object> params,Class<T> c){ List<T> list=new ArrayList<T>(); Connection con=getcon(); PreparedStatement pstm=null; ResultSet rs=null; try { pstm=con.prepareStatement(sql); doParams(pstm,params); //得到结果集 rs=pstm.executeQuery(); //1、通过反射得到所有的方法 Method[] ms=c.getMethods(); ResultSetMetaData md=rs.getMetaData(); //存储列名 String[] columnName=new String[md.getColumnCount()]; //获取列名 for(int i=0;i<columnName.length;i++){ columnName[i]=md.getColumnName(i+1); } T t; String mname=""; //方法名 String cname=""; //列名 String ctypeName=""; //类型名 //根据列名获取值 while(rs.next()){ //创建反射类的实例对象 t=(T)c.newInstance(); //循环方法名,找到setXXX for(int i=0;i<columnName.length;i++){ cname=columnName[i]; cname="set"+cname.substring(0,1).toUpperCase()+cname.substring(1).toLowerCase(); if(ms!=null && ms.length>0){ //查找方法名 for(Method m:ms){ mname=m.getName(); //匹配 if(cname.equals(mname) && rs.getObject(columnName[i])!=null){ ctypeName=rs.getObject(columnName[i]).getClass().getName(); //判断数据库中数据的类型 if("java.lang.Integer".equals(ctypeName)){ m.invoke(t, rs.getInt(columnName[i])); //int型的另一种形式 }else if("java.math.BigDecimal".equals(ctypeName)){ try { m.invoke(t, rs.getInt(columnName[i])); } catch (Exception e) { m.invoke(t, rs.getDouble(columnName[i])); } }else if("java.lang.String".equals(ctypeName)){ m.invoke(t, rs.getString(columnName[i])); }else if("java.sql.TimeStamp".equals(ctypeName)){ m.invoke(t, rs.getString(columnName[i])); }else if("java.sql.Date".equals(ctypeName)){ m.invoke(t, rs.getDate(columnName[i])); }else if("image".equals(ctypeName)){ m.invoke(t, rs.getBlob(columnName[i])); }else if("oracle.sql.BLOB".equals(ctypeName)){ BufferedInputStream is=null; byte[] bytes=null; Blob blob=rs.getBlob(columnName[i]); try { is=new BufferedInputStream(blob.getBinaryStream()); bytes=new byte[(int) blob.length()]; is.read(bytes); } catch (IOException e) { e.printStackTrace(); } m.invoke(t, bytes); }else{ m.invoke(t, rs.getString(columnName[i])); } break; } } } } //添加 list.add(t); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } try { closeAll(rs,null,con,pstm); } catch (SQLException e) { e.printStackTrace(); } return list; } }</pre>
  • 发表于 2018-07-07 14:40
  • 阅读 ( 693 )
  • 分类:代码片段

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除