增删改查的工具类及sql语句参数插入和结果集获取的封装(待增加) /** * 为sql语句传入参数值 * @param ps 处理命令 * @param i 传入参数序号 * @param obj 传入 * */public static void insertArgs(Prepar
/** * 为sql语句传入参数值 * @param ps 处理命令 * @param i 传入参数序号 * @param obj 传入 * */ public static void insertArgs(PreparedStatement ps,int i,Object obj){ if(obj instanceof Integer){ try { ps.setInt(i,(Integer)obj); } catch (SQLException e) { e.printStackTrace(); } }else if(obj instanceof String){ try { ps.setString(i, (String)obj); } catch (SQLException e) { e.printStackTrace(); } }else if(obj instanceof Double){ try { ps.setDouble(i, (Double)obj); } catch (SQLException e) { e.printStackTrace(); } } } /** * 返回结果集对应列的值 * @param type 通过反射获取的实体类属性类型 * @param rs 结果集 * @param str 实体类中和表字段对应的属性 * @return 返回结果集字段对应的值,发生异常返回null * */ public static Object getResultSet(String type,ResultSet rs,String str){ if(type.equalsIgnoreCase("int")||type.equalsIgnoreCase("Integer")){ try { return rs.getInt(str); } catch (SQLException e) { e.printStackTrace(); } }else if(type.equalsIgnoreCase("String")){ try { return rs.getString(str); } catch (SQLException e) { e.printStackTrace(); } } return null; } /** * 单表查询 * @param c 表对应对象class * @param slist 条件参数表达式集合(例:sno>? ; sno=? ; sno in(?,?)) * @param s_list 条件值集合 * */ @SuppressWarnings("all") public staticList singleSelectData(Class c,List slist,List s_list){ T t=null; List tlist=new ArrayList(); try { List list=new ArrayList (); for(Field fi:c.getDeclaredFields()){ list.add(fi.getName()); } PreparedStatement ps=getConn().prepareStatement(createSelectSql(c, list,slist)); for(int i=0;i ? ; sno=? ; sno in(?,?)) * @param s_list 按照条件参数表达式依次添加的值 * @param map 修改的参数和值对(key,String类型,表字段名;value,注意添加时类型与数据库一致,插入的值) * @return 插入成功返回1,失败返回0 * */ @SuppressWarnings("rawtypes") public static int alterData(Class t,List list,List slist,List s_list,Map map){ List alist=new ArrayList (); if(list.size()==1&&"*".equals(list.get(0))){ for(Field fi:t.getDeclaredFields()){ alist.add(fi.getName()); } }else alist=list; // System.out.println(createUpdataSql(t,alist, slist)); try { PreparedStatement ps=getConn().prepareStatement(createUpdataSql(t,alist, slist)); for(int i=0;i map1:map.entrySet()){ if(alist.get(i).equals(map1.getKey())){ if(map1.getValue() instanceof String) ps.setString(i+1, map1.getValue().toString()); else ps.setInt(i+1,(Integer)map1.getValue()); } } } for(int k=0;k ? ; sno=? ; sno in(?,?)) * @return 插入成功返回1,失败返回0 * */ @SuppressWarnings("rawtypes") public static int deleteData(Class t,List slist,List list){ //System.out.println(createDeleteSql(t, list)); try { PreparedStatement ps=getConn().prepareStatement(createDeleteSql(t, list)); for(int i=0;i int insertData(Class t,Map map){ List list=new ArrayList (); for(Field me:t.getDeclaredFields()){ list.add(me.getName()); } try { // System.out.println(createInsertSql(t, list)); PreparedStatement ps=getConn().prepareStatement(createInsertSql(t, list)); for(int i=0;i m1:map.entrySet()){ if(list.get(i).equals(m1.getKey())){ if(m1.getValue() instanceof String){ // System.out.println("String"+m1.getValue().toString()); ps.setString(i+1,m1.getValue().toString()); break frist; }else if(m1.getValue() instanceof Integer){ // System.out.println("int"+Integer.parseInt(m1.getValue().toString())); ps.setInt(i+1, Integer.parseInt(m1.getValue().toString())); break frist; } } } } return ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } return 0; }