vallllll
vallllll

Reputation: 2761

Problem inserting into an SQL Server Database with Java

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

Upvotes: 2

Views: 1360

Answers (1)

Bohemian
Bohemian

Reputation: 425278

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.

Upvotes: 1

Related Questions