0

当我将“ALTER TABLE”添加到我的表时,我在将数据添加到我的数据库时遇到问题。我正在从正在处理的网页中获取信息,并被写入将信息传输到我的数据库的 java 方法。如果我不使用这两个 ALTER TABLE 语句,一切正常,但是一旦添加它们,信息将不再进入我的数据库。我究竟做错了什么?下面列出了 CREATE TABLE 和 Java 方法。希望可以有人帮帮我!

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方法:

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;
}
4

1 回答 1

0

您必须先插入roleandplace才能插入customer,否则将违反您的参照完整性。

这两个ALTER TABLE语句意味着customer.username必须有一个对应的值role.username并且customer.zipcode必须指向一个有效的place.zipcode

当您插入第customer一个时,这些记录尚不存在。

编辑:

executeUpdate更改to的顺序

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

应该做的伎俩。

编辑2:

不过要注意的一件事是,如果您有两个人使用相同的邮政编码,您的代码将会失败,因为place如果您尝试添加两次,您的插入将违反主键......

于 2013-03-14T00:45:51.673 回答