Cant create table with preparedStatement

前端 未结 2 648
情书的邮戳
情书的邮戳 2021-01-13 17:06

I can\'t create a table in the database (mySQL), using preparedStatement and try to enter name of future table with preparedStatement.setInteger():

相关标签:
2条回答
  • 2021-01-13 17:52

    You will have to format the string after reading the table name, something like:

    static String queryCreateTable = "CREATE TABLE {0}" +
                                     "(ID INTEGER not NULL ," +
                                     "BRAND VARCHAR(40)," +
                                     "MODEL VARCHAR(40)," +
                                     "YEAR INTEGER not NULL," +
                                     "NOVELTY BINARY," +
                                     "PRIMARY KEY ( ID ))";
    

    then create like:

    newNameOfTable = JOptionPane.showInputDialog("Connected for saving data. " +
                                "Input name of new table:");
    
    statement = connection.createStatement();
    statement.execute(MessageFormat.format(queryCreateTable, newNameOfTable));
    
    0 讨论(0)
  • 2021-01-13 17:53
    newNameOfTable = JOptionPane.showInputDialog("Connected for saving data. " +
                                    "Input name of new table:");
    
    static String queryCreateTable = "CREATE TABLE " + newNameOfTable +
                                     "(ID INTEGER not NULL ," +
                                     "BRAND VARCHAR(40)," +
                                     "MODEL VARCHAR(40)," +
                                     "YEAR INTEGER not NULL," +
                                     "NOVELTY BINARY," +
                                     "PRIMARY KEY ( ID ))";
    
    
    pStatement = connection.prepareStatement(queryCreateTable);
    pStatement.executeUpdate();
    

    PreparedStatement example: http://tutorials.jenkov.com/jdbc/preparedstatement.html

    0 讨论(0)
提交回复
热议问题