【问题标题】:java.sql.SQLIntegrityConstraintViolationException Servletjava.sql.SQLIntegrityConstraintViolationException Servlet
【发布时间】:2014-12-26 23:11:02
【问题描述】:

我正在尝试使用 DAO 模式创建一个简单的注册 Servlet,当我尝试将数据添加到数据库时出现异常。似乎 ID 出于某种原因没有获得价值,但为什么呢?

integrity constraint violation: 
java.sql.SQLIntegrityConstraintViolationException NOT NULL check constraint; 
SYS_CT_10092 table: CUSTOMER column: ID

数据库架构(使用 hsqldb):

CREATE SEQUENCE seq1 AS INTEGER START WITH 1;

CREATE TABLE customer (
     id BIGINT NOT NULL PRIMARY KEY,
     first_name VARCHAR(255) NOT NULL,
     surname VARCHAR(255) NOT NULL,
     code VARCHAR(255) NOT NULL,
);
INSERT INTO customer VALUES(NEXT VALUE FOR seq1,'Jane','Doe','123'); --test data

向数据库插入数据的Dao方法:

public void addCustomer(Customers c) {
    try {
        pst = getConnection().prepareStatement("insert into customer(first_name,surname,code)"
                + " values(?,?,?)");

        pst.setString(1, c.getFirst_name());
        pst.setString(2, c.getSurname());
        pst.setString(3, c.getCode());
        pst.executeUpdate();

    } catch(Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeResources();
    }
} 

在servlet类中调用dao方法:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    CustomerDao dao = new CustomerDao();
    String firstname = request.getParameter("firstName");
    String lastName = request.getParameter("lastName");
    String code = request.getParameter("code");

    Customers customer = new Customers();
    customer.setFirst_name(firstname);
    customer.setSurname(lastName);
    customer.setCode(code);
    dao.addCustomer(customer);
}  

【问题讨论】:

    标签: java servlets jdbc hsqldb


    【解决方案1】:

    看起来您需要将 id 列创建为 IDENTITY 列并在准备好的语句中插入 null:

    标识自动递增列

    每个表可以包含一个自增列,称为 IDENTITY 列。 IDENTITY 列始终被视为主要列 表的键(因此,多列主键不是 可能存在 IDENTITY 列)。已添加支持 CREATE TABLE ( IDENTITY, ...) 作为快捷方式。

    从 1.7.2 开始,默认使用 SQL 标准语法,允许 要指定的初始值。支持的形式是( 默认生成的整数作为标识(以 n 开头,[递增 m]) 主键,...)。还添加了对 BIGINT 身份的支持 列。因此,IDENTITY 列只是一个 INTEGER 或 BIGINT 列及其由序列生成的默认值 生成器。

    当您使用 INSERT INTO 向此类表添加新行时 ...;语句,您可以将 NULL 值用于 IDENTITY 列,这会为 柱子。 IDENTITY() 函数返回最后插入的值 此连接的任何 IDENTITY 列。使用 CALL IDENTITY();作为 SQL 语句来检索此值。如果您想将值用于 子表中的字段,您可以使用 INSERT INTO VALUES (...,身份(),...);。必须对 IDENTITY() 进行两种类型的调用 在任何其他更新或插入语句发出之前 数据库。

    下一个要使用的 IDENTITY 值可以使用

    ALTER TABLE ALTER COLUMN RESTART WITH ;

    http://hsqldb.org/doc/guide/ch02.html#N104B3

    【讨论】:

      【解决方案2】:

      根据 CREATE SEQUENCE seq1 AS INTEGER START WITH 1;

      pst = getConnection().prepareStatement("插入客户(first_name,surname,code)" + "值(?,?,?)");

      必须是:

      pst = getConnection().prepareStatement("插入客户(first_name,surname,code)" +“值(seq1的下一个值,?,?,?)”);

      【讨论】:

        猜你喜欢
        • 2021-05-27
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 2019-07-10
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 2018-02-20
        相关资源
        最近更新 更多