Exemplo n.º 1
0
  public static <T> List<T> select(String sql, Class<T> type) {

    try {
      start("");
      ResultSet rs = stmt.executeQuery(sql);
      ResultSetMetaData metaData = rs.getMetaData();
      int colCount = metaData.getColumnCount();

      String[] columns = new String[colCount];

      for (int i = 0; i < colCount; i++) {
        columns[i] = new String(metaData.getColumnLabel(i + 1));
      }
      List<T> list = new ArrayList<T>();

      while (rs.next()) {
        T obj = type.newInstance();
        for (Field f : type.getDeclaredFields()) {
          if (Modifier.isPrivate(f.getModifiers())) {
            String input = f.getName();
            input = input.substring(0, 1).toUpperCase() + input.substring(1);
            try {
              Class<?> type2 = f.getType();
              // System.out.println(type2);
              if (type2 == Integer.class) {

              } else {
                type2 = String.class;
              }
              Method met = type.getMethod("set" + input, type2);

              if (input.equals("Tarih")) {
                met.invoke(obj, rs.getString(f.getName()));
              }
              met.invoke(obj, rs.getObject(f.getName()));
            } catch (NoSuchMethodException e) {

              e.printStackTrace();
            } catch (Exception e) {
            }
          }
        }
        list.add(obj);
      }
      return list;
    } catch (SQLException se) {
      System.out.println("se in select<T" + sql);
      se.printStackTrace();
      return new ArrayList<T>();
    } catch (Exception e) {
      System.out.println("e in select");
      e.printStackTrace();
      return new ArrayList<T>();
    } finally {
      close("");
    }
  }
Exemplo n.º 2
0
  public List<DetallesVenta> execute() {

    Connection connection = null;
    DetallesVentaGateway venta = PersistenceFactory.getDetallesVentaGateaway();
    List<DetallesVenta> lista = null;

    try {

      connection = Jdbc.getConnection();
      venta.setConnection(connection);
      lista = venta.listAll();

    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      Jdbc.close(connection);
    }
    return lista;
  }