Question

I am having problems adding data to my database when I am adding "ALTER TABLE" to my tables. I am getting the information from a web page I am working on and is written to a java method that transfers the information to my Database. Everything works fine if I don't use the two ALTER TABLE sentences, but as soon they are added the information will no longer go to my Database. What am I doing wrong? The CREATE TABLE and Java method is listed below. Hope someone can help me!

CREATE TABLE role(
username VARCHAR(15) NOT NULL,
password VARCHAR(15) NOT NULL,
role VARCHAR(6) NOT NULL,
CONSTRAINT username_pk PRIMARY KEY (username)
);

CREATE TABLE customer(
orgnumber INTEGER NOT NULL,
companyname VARCHAR(20) NOT NULL,
contactperson VARCHAR(20),
streetname VARCHAR(30) NOT NULL,
zipcode INTEGER NOT NULL,
city VARCHAR(15) NOT NULL,
phone CHAR(12),
email VARCHAR(30) NOT NULL,
username VARCHAR(15),
CONSTRAINT orgnumber_pk PRIMARY KEY (orgnumber)
);

CREATE TABLE place(
zipcode INTEGER NOT NULL,
city VARCHAR(15),
streetname VARCHAR(30),
CONSTRAINT place_pk PRIMARY KEY (zipcode)
);

ALTER TABLE customer
    ADD CONSTRAINT role_fk1 FOREIGN KEY (username)
        REFERENCES role;

ALTER TABLE customer
    ADD CONSTRAINT place_fk1 FOREIGN KEY (zipcode)
        REFERENCES place;

Java method:

public boolean regNewRegister(RegBean newRegister) {
    PreparedStatement sqlnewRegister = null;
    PreparedStatement sqlnewRole = null;
    PreparedStatement sqlnewPlace = null;
    String knd = "Customer";

    OpenConnection();
    boolean ok = false;
    try {
        /*
         * A transaction is started, uses lock.
         */
        if (connection == null) {
            System.out.println("Went well");
        }
        connection.setAutoCommit(false);

        sqlnewRegister = connection.prepareStatement("insert into customer (ORGNUMBER, CNAME, CONTACTP, STREETN, ZIPC, CITY, PHONE, EMAIL, USERNAME) values(?, ?, ?, ?, ?, ?, ?, ?, ?)");
        sqlnewRegister.setInt(1, newRegister.getOrgNumber());
        sqlnewRegister.setString(2, newRegister.getCompanyName());
        sqlnewRegister.setString(3, newRegister.getContactPerson());
        sqlnewRegister.setString(4, newRegister.getStreetName());
        sqlnewRegister.setInt(5, newRegister.getZipCode());
        sqlnewRegister.setString(6, newRegister.getCity());
        sqlnewRegister.setLong(7, newRegister.getPhone());
        sqlnewRegister.setString(8, newRegister.getEmail());
        sqlnewRegister.setString(9 newRegister.getUsername());

        sqlnewRole = connection.prepareStatement("insert into role (USERNAME, PASSWORD, ROLE) values (?, ?, ?)");
        sqlnewRole.setString(1, newRegister.getUsername());
        sqlnewRole.setString(2, newRegister.getPassword());
        sqlnewRole.setString(3, knd);

        sqlnewPlace = connection.prepareStatement("insert into place (ZIPC, CITY, STREETN) values (?, ?, ?)");

        sqlnewPlace.setInt(1, newRegister.getZipCode());
        sqlnewPlace.setString(2, newRegister.getCity());
        sqlnewPlace.setString(3, newRegister.getStreetName());

        sqlnewRegister.executeUpdate();
        sqlnewRole.executeUpdate();
        sqlnewPlace.executeUpdate();

        connection.commit();

        /*
         * Transaction ended
         */
        ok = true;

    } catch (SQLException e) {
        Cleaner.rollBack(connection);
        String sqlStatus = e.getSQLState().trim();
        String statusclass = sqlStatus.substring(0, 2);
        if (statusclass.equals("23")) { // Standard code for "integrity constraint violation"
            ok = false;  // This orgnumber is already registered
        } else {
            Cleaner.writeMessage(e, "WriteToDB");
        }
    } finally {
        Cleaner.settAutoCommit(connection);
        Cleaner.closeSentence(sqlnewRegister);
        Cleaner.closeSentence(sqlnewRole);
        Cleaner.closeSentence(sqlnewPlace);
    }
    closeConnection();
    return ok;
}
Was it helpful?

Solution

You'll have to insert the role and place before you can insert the customer, as otherwise your referential integrity will be violated.

The two ALTER TABLE statements mean that customer.username must have a corresponding value role.username and customer.zipcode must point to a valid place.zipcode.

As you're inserting the customer first, those records won't exist yet.

EDIT:

Changing the order of executeUpdate to

    sqlnewRole.executeUpdate();
    sqlnewPlace.executeUpdate();
    sqlnewRegister.executeUpdate();

should do the trick.

EDIT2:

One thing to note though, your code will fall over if you have two people with the same ZIP code, as your insert to place will violate the primary key if you try adding it twice...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top