Tuesday 15 February 2011

java.sql.SQLIntegrityConstraintViolationException Servlet -



java.sql.SQLIntegrityConstraintViolationException Servlet -

i'm trying create simple registration servlet using dao pattern, exception comes, when seek add together info database. seems id doesnt value reason why?

integrity constraint violation: java.sql.sqlintegrityconstraintviolationexception not null check constraint; sys_ct_10092 table: client column: id

database schema (using hsqldb):

class="lang-sql prettyprint-override">create sequence seq1 integer start 1; create table client ( id bigint not null primary key, first_name varchar(255) not null, surname varchar(255) not null, code varchar(255) not null, ); insert client values(next value seq1,'jane','doe','123'); --test info

dao method inserting info database :

public void addcustomer(customers c) { seek { pst = getconnection().preparestatement("insert 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); } { closeresources(); } }

calling dao method in servlet class :

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 client = new customers(); customer.setfirst_name(firstname); customer.setsurname(lastname); customer.setcode(code); dao.addcustomer(customer); }

looks need create id column identity column , insert null in prepared statement:

identity auto-increment columns

each table can contain 1 auto-increment column, known identity column. identity column treated primary key table (as result, multi-column primary keys not possible identity column present). back upwards has been added create table ( identity, ...) shortcut.

since 1.7.2, sql standard syntax used default, allows initial value specified. supported form is( integer generated default identity(start n, [increment m])primary key, ...). back upwards has been added bigint identity columns. result, identity column integer or bigint column default value generated sequence generator.

when add together new row such table using insert ...; statement, can utilize null value identity column, results in auto-generated value column. identity() function returns lastly value inserted identity column connection. utilize phone call identity(); sql statement retrieve value. if want utilize value field in kid table, can utilize insert values (...,identity(),...);. both types of phone call identity() must made before additional update or insert statements issued on database.

the next identity value used can set the

alter table alter column restart ;

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

java servlets jdbc hsqldb

No comments:

Post a Comment