簡體   English   中英

Java JDBC executeUpdate用法

[英]Java JDBC executeUpdate usage

這是Web應用程序的一部分,該Web應用程序使用GUI獲取用戶輸入並在MySQL中創建關系表。 問題是為什么不創建表? 使用調試器,問題出在

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

謝謝。

它應該是 :

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示例嗎?

問題在於邏輯。 代替

tableCreation.append(charTF.getValue()).append(" ");

它應該是

tableCreation.append("(").append(charTF.getValue()).append(")").append(" ");

回答我自己的問題,沒有錯

st.executeUpdate(tableCreation.toString());

首先它不起作用的原因是因為創建表的語法一開始是錯誤的。 因此,靜態字符串表有效,而邏輯創建的字符串無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM