org.postgresql.util.PSQLException:错误:该关系“ test_id”不存在。 SQL状态:42883

时间:2019-07-07 16:52:45

标签: postgresql jdbc

在通过pgadmin4将其保存在postgresql中之后,我无法从“测试”表中检索ID:

这里是pgadmi4的“ test”表生成的创建脚本:

stopAnimation()

以下代码将值插入到thourgh JDBC的“测试”表中:

CREATE TABLE school.test
(
    test_id bigint NOT NULL DEFAULT nextval('school.test_test_id_seq'::regclass),
    name character varying(10) COLLATE pg_catalog."default"
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE school.test
    OWNER to postgres;

这里是获取受影响表的最后一个序列号的函数:

        Connection conn = pstgConn.dbConnection();
        String query = "INSERT INTO school.test(name) VALUES (?)";
        try (PreparedStatement pst = conn.prepareStatement(query)) {
            pst.setString(1, "anything");
            pst.executeUpdate();
            //Get Sequential number of the table
            getSerialNum(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

我尝试通过pdgadmin控制台独立运行,结果相同。

结果在这里:

org.postgresql.util.PSQLException:错误:关系“ test_id”不存在   位置:16     在org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)     在org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)     在org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)     在org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)     在org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)     在org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)     在com.cgi.training.javase.school.dao.testDAO.getSerialNum(testDAO.java:45)     在com.cgi.training.javase.school.dao.testDAO.main(testDAO.java:21)

1 个答案:

答案 0 :(得分:0)

@a_horse_with_no_name的响应:您的序列名为test_test_id_seq,但是您将名称test_id传递给currval()的调用– a_horse_with_no_name