2

I have a problem because I can't insert into an sql server database:

driver:[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]

public int sendCabecera(String tpv, Date date, String time, int NumPers, int CodOfi) {

    java.sql.DatabaseMetaData dm = null;
    int result = 0;

    try {
        connection = this.getConnection();
        if (connection != null) {
            dm = connection.getMetaData();

            Statement select = connection.createStatement();


            String sql="INSERT INTO TCabecerasSolicitudes(CodigoTPV, FechaYHora, Hora, NumeroPersonas, CodigoOficina) VALUES ('001','17/08/2011 16:00','De 16 a 17',0, '001');";

            select.execute(sql);


//{.... catch ... etc }

Well I have tried all execute update and execute alone and I always het the same error: "Cannot insert NULL" this is the first comlumn which is a primary key and normally auto-increment so I just ignore it but it doesn't work why???

Thanks a lot in advance,

EDIT: problem solved I turned out the key in question was not autoincrement

4
  • Post the full table definition please. Commented Aug 18, 2011 at 8:03
  • 5
    Are you sure that the SQL works if you execute it manually? JDBC should be broadly equivalent to just executing the same statement in a Sql Server Management Studio window... Commented Aug 18, 2011 at 8:04
  • 2
    configure your column for your primary key with autoincrement and a start value. A good example for a start value is Zero. Or you let the cms create for it own, using the UUID format. Commented Aug 18, 2011 at 8:06
  • sorry guys the mistake was from the database, it is solved now thank you anyway Commented Aug 18, 2011 at 9:22

1 Answer 1

1

It seems most likely that you have a column in your TCabecerasSolicitudes table that is defined as NOT NULL but which is not listed in your insert statement (ie not one of CodigoTPV, FechaYHora, Hora, NumeroPersonas, CodigoOficina).

Because you have not provided a value for that column in your query and it's NOT NULL, the statement is exploding.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.