2016-04-27 35 views
3

在JOOQ documentation它說我可以這樣做:JOOQ的Java 8架構是抽象的,不能被實例化

try (Connection c = getConnection()) { 
    String sql = "select schema_name, is_default " + 
       "from information_schema.schemata " + 
       "order by schema_name"; 

    DSL.using(c) 
     .fetch(sql) 

     // We can use lambda expressions to map jOOQ Records 
     .map(rs -> new Schema(
      rs.getValue("SCHEMA_NAME", String.class), 
      rs.getValue("IS_DEFAULT", boolean.class) 
     )) 

     // ... and then profit from the new Collection methods 
     .forEach(System.out::println); 
} 

然而,當我這樣做,我得到的錯誤「org.jooq.Schema是抽象的;不能實例化「 - 如果你看documentation這是真的。

那麼這個例子中的代碼應該如何工作呢?

回答

3

簡短回答:他們在他們的示例中沒有使用"org.jooq.Schema",而是使用靜態內部類。


如果您在the page you linked底部向下滾動,他們給github上鍊接的例子。你有的例子是SQL goodies之一。

如果你打開SQLGoodies.java你會在示例類

static class Schema { 
    final String schemaName; 
    final boolean isDefault; 

    Schema(String schemaName, boolean isDefault) { 
     this.schemaName = schemaName; 
     this.isDefault = isDefault; 
    } 

    @Override 
    public String toString() { 
     return "Schema{" + 
       "schemaName='" + schemaName + '\'' + 
       ", isDefault=" + isDefault + 
       '}'; 
    } 
} 

隨後的頂端有一個靜態內部類Schema向下滾動,你會用內部類找到你的例子:

DSL.using(c) 
    .fetch(sql) 
    .map(r -> new Schema(
      r.getValue("SCHEMA_NAME", String.class), 
      r.getValue("IS_DEFAULT", boolean.class) 
    ))