Esempio n. 1
0
 /**
  * 查询语句的操作
  *
  * @param sql 查询语句
  * @param objects 查询语句的参数
  * @return 结果集
  */
 public ResultSet select(String sql, Object... objects) {
   try {
     conn = DBManager.getConnection();
     pstm = conn.prepareStatement(sql);
     if (objects != null && objects.length > 0) {
       for (int i = 0; i < objects.length; i++) {
         pstm.setObject(i + 1, objects[i]);
       }
     }
     rs = pstm.executeQuery();
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return rs;
 }
Esempio n. 2
0
 /**
  * 根据条件查找信息
  *
  * @param sql
  * @param t
  * @return
  */
 public List findListTByCondition(String sql, T t) {
   List<T> list = new ArrayList<T>();
   T vo = null;
   StringBuffer bsql = new StringBuffer(sql);
   bsql.append(" 1=1 ");
   Class tclazz = t.getClass();
   Field[] fields = t.getClass().getDeclaredFields(); // 获得属性
   try {
     for (Field field : fields) {
       conn = DBManager.getConnection();
       PropertyDescriptor pd = new PropertyDescriptor(field.getName(), tclazz);
       // 通过反射获取get方法的属性
       Method getMethod = pd.getReadMethod(); // 执行get方法返回一个Object
       Object o = getMethod.invoke(t); // 执行get方法返回一个Object
       if (o != null) {
         // 生成根据信息查询的sql语句
         bsql.append(" and ").append(field.getName());
         bsql.append("='").append(o).append("'");
       }
     }
     System.out.println(bsql);
     pstm = conn.prepareStatement(bsql.toString());
     rs = pstm.executeQuery();
     // 根据结果集返回集合
     while (rs.next()) {
       vo = autoBeanByRs(rs);
       list.add(vo);
     }
   } catch (SQLException ex) {
     Logger.getLogger(BasicDao.class.getName()).log(Level.SEVERE, null, ex);
   } catch (IllegalAccessException ex) {
     Logger.getLogger(BasicDao.class.getName()).log(Level.SEVERE, null, ex);
   } catch (IllegalArgumentException ex) {
     Logger.getLogger(BasicDao.class.getName()).log(Level.SEVERE, null, ex);
   } catch (InvocationTargetException ex) {
     Logger.getLogger(BasicDao.class.getName()).log(Level.SEVERE, null, ex);
   } catch (IntrospectionException ex) {
     Logger.getLogger(BasicDao.class.getName()).log(Level.SEVERE, null, ex);
   }
   DBManager.close(conn, pstm, rs);
   return list;
 }
Esempio n. 3
0
 /**
  * 非查询语句的操作
  *
  * @param sql 非查询的sql语句
  * @param objects 非查询语句的参数
  * @return 受影响的行数
  */
 public int executeUpdatePerparement(String sql, Object... objects) {
   int row = 0;
   try {
     conn = DBManager.getConnection();
     // 创建prepareStatement ,预编译sql语句
     pstm = conn.prepareStatement(sql);
     if (objects != null && objects.length > 0) {
       for (int i = 0; i < objects.length; i++) {
         // 使用给定对象设置指定参数的值。
         pstm.setObject(i + 1, objects[i]);
       }
     }
     // 执行语句
     row = pstm.executeUpdate();
     DBManager.close(conn, pstm, rs);
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return row;
 }
Esempio n. 4
0
 /**
  * 返回一个实体类
  *
  * @param sql 查询的sql语句
  * @param t 传入的实体类
  * @param objects 查询语句的参数
  * @return 实体类
  */
 public T findByPredStatmentT(String sql, Object... objects) {
   T vo = null;
   try {
     conn = DBManager.getConnection();
     pstm = conn.prepareStatement(sql);
     if (objects != null && objects.length > 0) {
       for (int i = 0; i < objects.length; i++) {
         pstm.setObject(i + 1, objects[i]);
       }
     }
     rs = pstm.executeQuery();
     while (rs.next()) {
       vo = autoBeanByRs(rs);
     }
   } catch (SQLException e) {
     e.printStackTrace();
   }
   DBManager.close(conn, pstm, rs);
   return vo;
 }
Esempio n. 5
0
 /**
  * 根据sql语句查找数据库中的记录
  *
  * @param sql 要查找的Sql语句
  * @param objects 传入的参数
  * @return 返回的List集合
  */
 public List<T> findListT(String sql, Object... objects) {
   List<T> list = new ArrayList<T>();
   T vo = null;
   try {
     conn = DBManager.getConnection();
     pstm = conn.prepareStatement(sql);
     if (objects != null && objects.length > 0) {
       for (int i = 0; i < objects.length; i++) {
         pstm.setObject(i + 1, objects[i]);
       }
     }
     rs = pstm.executeQuery();
     while (rs.next()) {
       vo = autoBeanByRs(rs);
       list.add(vo);
     }
   } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   DBManager.close(conn, pstm, rs);
   return list;
 }