これは、GUI を使用してユーザー入力を取得し、MySQL でリレーショナル テーブルを作成する Web アプリケーションの一部です。問題は、テーブルが作成されないのはなぜですか? デバッガを使用すると、問題は
st.executeUpdate(tableCreation.toString());
だから私はこれをやってみました
String[] Tables = {
tableCreation.toString()
};
for (int i = 0; i < Tables.length; ++i) {
st.executeUpdate(Tables[i]);
}
しかし、それもうまくいきません。上記にした理由は、静的配列 String を使用したときにテーブルが作成されるためです
static String[] Tables = {
"create table STATE (" +
"ABBREVIATION char(2) not null, " +
"NAME varchar(32) not null, " +
"ENTERED_UNION date null, " +
"CAPITAL varchar(32) not null, " +
"REGION varchar(16) not null, " +
"AREA int not null, " +
"FLOWER varchar(32) null, " +
"BIRD varchar(32) null)"
};
静的テーブルを使用すると機能します。だから私は何がうまくいかなかったのだろうか?
public String createButton1_action() {
StringBuilder tableCreation = new StringBuilder();
tableCreation.append("create table ").append(tableTF.getValue()).append(" (");
StringBuilder primaryKey = new StringBuilder();
for (int i = 0; i < column; i++) {
// first get() returns the gridPanel, second get() returns the components within the panel
TextField tempTF = (TextField) gridPanels.get(i).getChildren().get(1);
// the table name from textfield
tableCreation.append(tempTF.getValue()).append(" ");
// the data type
DropDown tempDP = (DropDown) gridPanels.get(i).getChildren().get(3);
tableCreation.append(tempDP.getValue()).append(" ");
// char/varchar amount
if (tempDP.getValue().equals("CHAR") || tempDP.getValue().equals("VARCHAR")) {
TextField charTF = (TextField) gridPanels.get(i).getChildren().get(4);
tableCreation.append(charTF.getValue()).append(" ");
}
// primary key
tempDP = (DropDown) gridPanels.get(i).getChildren().get(6);
addPK(tempTF, tempDP, primaryKey);
// nullable?
tempDP = (DropDown) gridPanels.get(i).getChildren().get(8);
tableCreation.append(tempDP.getValue());
tableCreation.append(", ");
}
tableCreation.append("PRIMARY KEY (").append(primaryKey).append(")");
tableCreation.append(")");
String[] Tables = {
tableCreation.toString()
};
try {
// Get a connection from the connection factory
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB", "root", "onfigii");
// Create a Statement object so we can submit SQL statements to the driver
Statement st = con.createStatement();
// Submit the statement (Doesn't work too. It worked with a static table)
for (int i = 0; i < Tables.length; ++i) {
st.executeUpdate(Tables[i]);
}
// Why doesn't this work?
st.executeUpdate(tableCreation.toString());
// Close the statement
st.close();
// Close the connection
con.close();
} catch (SQLException ex) {
Logger.getLogger(CreatePage.class.getName()).log(Level.SEVERE, null, ex);
}
tableTF.setText(null);
for (int i = 0; i < column; i++) {
TextField tempTF = (TextField) gridPanels.get(i).getChildren().get(1);
tempTF.setValue(null);
}
getApplicationBean1().refreshDataProvider();
return null;
}
ありがとうございました。