java枚举和postgresql枚举 [英] java enum and postgresql enum

查看:89
本文介绍了java枚举和postgresql枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CREATE TABLE customers
(
  first_name character varying(15),
  second_name character varying(20),
  login character varying(15) NOT NULL,
  password character varying(15),
  email character varying(40),
  gender gender,
  register_date date,
  date_of_birth date,
  address character varying(40),
  address_number integer,
  town character varying(20),
  CONSTRAINT login PRIMARY KEY (login)
)

我有此表,并创建了一个性别枚举,例如:

I have this table and I created an enum of gender such as:

CREATE TYPE gender AS ENUM ( 'F', 'M',);

我正在尝试使用PreparedStatement
从eclipse java插入客户数据,但是有一个错误,例如ERROR:性别列的性别类型,但表达式的字符类型不同
提示:您将需要重写或强制转换表达式。

I am trying to insert into customers data from eclipse java with PreparedStatement but there is an error such as ERROR: column "gender" is of type gender but expression is of type character varying Hint: You will need to rewrite or cast the expression.

我的Java代码如下:

My Java code looks like:

PreparedStatement pre_state;

public enum gendertype {
    F,
    M;
}

pre_state = conn.prepareStatement("INSERT INTO"
            + " customers VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
pre_state.set(6, gendertype.F.toString());


推荐答案

我对这个答案一无所知

PostgreSQL给出了答案

PostgreSQL provides the answer when it says


提示:您将需要重写或强制转换表达式

Hint: You will need to rewrite or cast the expression

Java代码正在创建一个字符串文字值,表示Java枚举类型类型。

The Java code is creating a string literal value that represents the Java enum gendertype type.

通过在值 :: gender

因此有效输入应为

'F'::gender

'M'::gender

这是因为所有PostgreSQL类型具有一个输入方法,该输入方法采用文本表示形式并将其转换为内部形式。

This works because all PostgreSQL types have a input method that takes a text representation and converts that to the internal form.

这篇关于java枚举和postgresql枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆