【问题标题】:POSTGRES SQL JAVA create custom type with a java classPOSTGRESQL JAVA 使用 java 类创建自定义类型
【发布时间】:2017-10-07 01:50:19
【问题描述】:
public static void main(String[] args) {
    try {
        Connection c = null;
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/postgres", // db
                "postgres", // account
                "ForOnlyOneTick1" // password
        );


        Object loc = Dimension.class;
        PreparedStatement insertNew = c
                .prepareStatement("CREATE TYPE loc AS (?);");
        insertNew.setObject(1, loc);

        insertNew.executeUpdate();
        insertNew.close();

        c.close();

    } catch (Exception e) {
        //e.printStackTrace();
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }

}

在那个例子中,我尝试在我的数据库中创建一个维度类型。 但它不起作用。

是否可以使用特定类型的 java 类创建列?

或者我可以只使用我的数据库知道的类型吗?

例如:

CREATE TYPE location AS (x int, y int, z int, name VARCHAR(15));

好的,但是如果我需要创建一个类型,问题仍然存在:

CREATE TYPE land AS (area Dimension, people ListPeople, probability int, cause Causes);

Dimension,ListPeople,Causes 是我的自定义类,其他类具有公共值。

【问题讨论】:

    标签: java sql database postgresql types


    【解决方案1】:

    根据这个http://rob.conery.io/2014/11/03/using-custom-types-with-postgres/,您可以使用现有的基本类型创建自定义类型,您的自定义类型将看起来像您的类。

    【讨论】:

      【解决方案2】:

      数据库对 Java 类一无所知。在定义数据类型时,您需要明确列出所有列。

      你可以这样做:

      CREATE TYPE dimension AS (x int, y int);
      CREATE TYPE listpeople AS (names text[], etc.);
      CREATE TYPE land AS (area dimension, people listpeople, etc.); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-23
        • 1970-01-01
        • 2017-11-01
        • 1970-01-01
        • 2011-10-15
        • 2018-06-04
        • 1970-01-01
        • 2012-01-15
        相关资源
        最近更新 更多