这是使用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;
}
谢谢。
答案 0 :(得分:0)
它应该是:
create table a
(b DOUBLE default null,
c CHAR (10) default NULL,
d DATE NOT NULL,
e VARCHAR (20) default NULL,
PRIMARY KEY (b, d)
);
你能显示失败的sql示例吗?
答案 1 :(得分:0)
问题在于逻辑。而不是
tableCreation.append(charTF.getValue()).append(" ");
应该是
tableCreation.append("(").append(charTF.getValue()).append(")").append(" ");
回答我自己的问题,
没有错st.executeUpdate(tableCreation.toString());
它首先不起作用的原因是因为创建表的语法开头是错误的。因此,静态字符串表工作,而逻辑创建的字符串不起作用。