テーブルに「ALTERTABLE」を追加しているときに、データベースにデータを追加するときに問題が発生します。作業中のWebページから情報を取得しており、データベースに情報を転送するJavaメソッドに書き込まれています。2つのALTERTABLEセンテンスを使用しなくてもすべて正常に機能しますが、それらが追加されるとすぐに、情報はデータベースに移動しなくなります。私は何が間違っているのですか?CREATETABLEと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;
}