Я пытаюсь создать простую регистрацию сервлета с использованием шаблона DAO, возникает исключение, когда я пытаюсь добавить данные в базу данных. Кажется, что ID не получает значение по какой-то причине, но почему?java.sql.SQLIntegrityConstraintViolationException Servlet
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();
}
}
Вызов метода дао в классе сервлета:
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);
}