Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JDBC执行更新使用_Java_Mysql_Jdbc - Fatal编程技术网

Java JDBC执行更新使用

Java JDBC执行更新使用,java,mysql,jdbc,Java,Mysql,Jdbc,这是web应用程序的一部分,它使用GUI获取用户输入,并在MySQL中创建关系表。问题是为什么不创建表?使用调试器时,问题会在 st.executeUpdate(tableCreation.toString()); 所以我试着这么做 String[] Tables = { tableCreation.toString() }; for (int i = 0; i < Tables.length; ++i) { st.executeUpdate(Tables[i

这是web应用程序的一部分,它使用GUI获取用户输入,并在MySQL中创建关系表。问题是为什么不创建表?使用调试器时,问题会在

st.executeUpdate(tableCreation.toString());
所以我试着这么做

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

for (int i = 0; i < Tables.length; ++i) {
     st.executeUpdate(Tables[i]);
}
当我使用静态表时,它可以工作。所以我想知道出了什么问题

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;
}
公共字符串createButton1_action(){
StringBuilder tableCreation=新建StringBuilder();
tableCreation.append(“创建表”).append(tableTF.getValue()).append(“”);
StringBuilder primaryKey=新建StringBuilder();
对于(int i=0;i
谢谢。

应该是:

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

它之所以不起作用,首先是因为创建表的语法一开始就错了。因此,静态字符串表工作,而逻辑创建的字符串不工作。

“不工作”既不是有效的MySQL错误消息,也不是有效的Java异常。您需要更加具体。@horse\u没有名称,这是一个SQLException错误。发生的情况是,从“st.executeUpdate(tableCreation.toString());”中,在ex中捕获了错误。例如,这是sql“创建表a(b双空,c字符10空,d日期不空,e VARCHAR 20空,主键(b,d)”JDBC驱动程序引发的每个异常都是一个
SQLException
question@MarkRotteveel,没有stacktrace。发生的情况是,在web中,我有另一个下拉列表,它是检索数据库中所有表存储的视图。该表从未插入。即使在调试模式下,我可以跟随它到将退出的错误处,然后继续返回以执行其余的代码,网页只需刷新即可。
st.executeUpdate(tableCreation.toString());