简体   繁体   中英

Java JDBC executeUpdate usage

This is part of a web application that get user input using GUI and creates a relational table in MySQL. The question is why is it that the table is not created? Using the debugger, the problem is during

st.executeUpdate(tableCreation.toString());

So I tried doing this

String[] Tables = {
        tableCreation.toString()
};

for (int i = 0; i < Tables.length; ++i) {
     st.executeUpdate(Tables[i]);
}

But it doesn't work too. The reason why I did the above is because the table is created when a static array String was used

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

When I used the static table, it works. So I am wondering what went wrong?

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

Thank you.

it should be :

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

can you show failed sql example?

The problem lies with the logic. Instead of

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

It should be

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

To answer my own question, there is nothing wrong with

st.executeUpdate(tableCreation.toString());

The reason why it didn't work in the first place was because the syntax for the creation of table was wrong to begin with. Hence the static String Table worked while the String created by logic didn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM