示例#1
0
文件: Query.java 项目: kptran/sql2o
  public Table executeAndFetchTable() {
    ResultSet rs;
    long start = System.currentTimeMillis();
    try {
      rs = statement.executeQuery();
      long afterExecute = System.currentTimeMillis();
      Table table = TableFactory.createTable(rs, this.isCaseSensitive());
      long afterClose = System.currentTimeMillis();

      logger.info(
          "total: {} ms, execution: {} ms, reading and parsing: {} ms; executed fetch table [{}]",
          new Object[] {
            afterClose - start,
            afterExecute - start,
            afterClose - afterExecute,
            this.getName() == null ? "No name" : this.getName()
          });

      return table;
    } catch (SQLException e) {
      throw new Sql2oException("Error while executing query", e);
    } finally {
      closeConnectionIfNecessary();
    }
  }
示例#2
0
文件: Query.java 项目: kptran/sql2o
  public <T> List<T> executeAndFetch(Class returnType) {
    List list = new ArrayList();
    PojoMetadata metadata =
        new PojoMetadata(returnType, this.isCaseSensitive(), this.getColumnMappings());
    try {
      // java.util.Date st = new java.util.Date();
      long start = System.currentTimeMillis();
      ResultSet rs = statement.executeQuery();
      long afterExecQuery = System.currentTimeMillis();

      ResultSetMetaData meta = rs.getMetaData();

      while (rs.next()) {

        Pojo pojo = new Pojo(metadata, this.isCaseSensitive());

        // Object obj = returnType.newInstance();
        for (int colIdx = 1; colIdx <= meta.getColumnCount(); colIdx++) {
          String colName = meta.getColumnName(colIdx);
          pojo.setProperty(colName, rs.getObject(colIdx));
        }

        list.add(pojo.getObject());
      }

      rs.close();
      long afterClose = System.currentTimeMillis();

      logger.info(
          "total: {} ms, execution: {} ms, reading and parsing: {} ms; executed [{}]",
          new Object[] {
            afterClose - start,
            afterExecQuery - start,
            afterClose - afterExecQuery,
            this.getName() == null ? "No name" : this.getName()
          });
    } catch (SQLException ex) {
      throw new Sql2oException("Database error", ex);
    } finally {
      closeConnectionIfNecessary();
    }

    return list;
  }