创建唯一的清单号(Java/PostgreSQL)
我正在研究菜单功能中的一个选项,它将要出售的汽车发布到数据库中。该选项要求用户输入年份、品牌、条件和价格,然后将其插入数据库中的car_sale表中。但是,在此选项期间还必须生成唯一的清单号。我无法定义我的表来唯一地生成10位数字的选项,但我必须对程序进行编码,以插入唯一生成的列表。\u否。下面您将找到我尝试这样做的代码,但是该代码仅在Oracle中有效,但我无法使用Oracle。我只能使用PostGreSQL和Java。因此,我的问题出现了,因为我使用的函数和关系不能用于PostGre 生成清单号的代码:创建唯一的清单号(Java/PostgreSQL),java,sql,postgresql,random,Java,Sql,Postgresql,Random,我正在研究菜单功能中的一个选项,它将要出售的汽车发布到数据库中。该选项要求用户输入年份、品牌、条件和价格,然后将其插入数据库中的car_sale表中。但是,在此选项期间还必须生成唯一的清单号。我无法定义我的表来唯一地生成10位数字的选项,但我必须对程序进行编码,以插入唯一生成的列表。\u否。下面您将找到我尝试这样做的代码,但是该代码仅在Oracle中有效,但我无法使用Oracle。我只能使用PostGreSQL和Java。因此,我的问题出现了,因为我使用的函数和关系不能用于PostGre 生成清
public int generateListingNo() throws SQLException
{
int listingSeq = 0;
Statement select = connection.createStatement();
result = select.executeQuery("select (to_char(sysdate,'yyyymmdd')||AUDIT_SEQ.NEXTVAL)valnext from dual");;
if(result.next())
{
listingSeq = result.getInt(1);
}
int seq = listingSeq;
return seq;
}
在选项函数中编写代码,以插入由generateListingNo()生成的lisitng_编号
我得到的错误是:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: relation "dual" does not exist
Position: 69 at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:217)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:421)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:318)
at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:281)
创建汽车销售表
create table car_sale(
listing_no int not null,
year varchar not null,
make varchar not null,
condition varchar not null,
price decimal(12,2) not null,
primary key (listing_no),
对于PostgreSQL,您必须以这种方式从java调用查询:
SELECT nextval('ACCOUNT_TRANSACTION_NO')
更改您对generateListingNo的查询,如下所示:
select q from (select (to_char(now(),'yyyymmdd') || NEXTVAL('AUDIT_SEQ') )q )sq
或
或在您的cocde上:
public int generateListingNo() throws SQLException
{
int listingSeq = 0;
Statement select = connection.createStatement();
result = select.executeQuery("select (to_char(now(),'yyyymmdd') || NEXTVAL('AUDIT_SEQ')) as newseqval");;
if(result.next())
{
listingSeq = result.getInt(1);
}
int seq = listingSeq;
return seq;
}
由于您没有顺序:
使用以下查询创建序列:
CREATE SEQUENCE public."AUDIT_SEQ"
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
或使用UUID:
public String generateListingNo() throws SQLException
{
return UUID.randomUUID().toString();
}
您的表结构需要更改:
create table car_sale(
listing_no varchar not null,
year varchar not null,
make varchar not null,
condition varchar not null,
price decimal(12,2) not null,
primary key (listing_no),
您得到的错误是什么。@RehanAzher我已在问题中编辑了错误。感谢您的帮助!您还可以发布表的create语句吗?您的代码似乎没有问题。@RehanAzher我已经编辑了这个问题,Op正在尝试将一个日期串联成10位数字。问题是在PostgreSql中未使用DUALdual。请确保已将Seq的起始值设置为超过2位。另外,我建议不要使用INT,而是使用BigInt,以防您的记录增长超过10位数。嗨,它不起作用。我收到错误audit\u seq不存在您是否可以尝试在if块中打印listseq的值。由于“audit\u seq relation not Existence”的错误,它将不允许我打印。我可以在PostGreSQL中使用它吗?是的,我已经在PG中测试过了,请检查你是否创建了序列。
public String generateListingNo() throws SQLException
{
return UUID.randomUUID().toString();
}
create table car_sale(
listing_no varchar not null,
year varchar not null,
make varchar not null,
condition varchar not null,
price decimal(12,2) not null,
primary key (listing_no),