从另一个表创建表时出现语法错误

时间:2023-01-19 18:24:50

My code:

我的代码:

    Statement stmt=null;
    String cmdstr = "create table " + tableName + " as (select * from Master_Sheet);";

        try{            
            stmt = con.createStatement();
            stmt.executeUpdate(cmdstr);

            }
        catch(Exception e)
            {
            e.printStackTrace();
            }
        finally
        {
            try{
                if(stmt != null)
                stmt.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

Output:

输出:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)

java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序] CREATE TABLE语句中的语法错误。 at sun.jdbc.odbc.JdbcOdbc.createSQLException(未知来源)at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)

Please help, i am very new to java coding.

请帮助,我是java编码的新手。

2 个解决方案

#1


3  

For Access, the syntax for creating a new table based on the data of a query is:

对于Access,基于查询数据创建新表的语法是:

SELECT INTO newTable
FROM oldTable;

So your code should be rewritten as:

因此,您的代码应该重写为:

String cmdstr = "insert into table " + tableName + " From Master_Sheet;";

Make sure that your SQL statement follow the MS Access syntax before you use them in your java code.

在Java代码中使用它们之前,请确保您的SQL语句遵循MS Access语法。

#2


2  

You're asking Access to create a table using this SQL statement:

您要求Access使用此SQL语句创建表:

create table yourTableName as (select * from Master_Sheet);

(including the ; at the end). It's telling you that's not valid syntax for a CREATE TABLE statement. This isn't a Java thing, it's an Access thing. See the linked CREATE TABLE documentation. (I linked the latest, be sure to find the one for the version of Access you're using.)

(包括;最后)。它告诉你这不是CREATE TABLE语句的有效语法。这不是Java的东西,它是Access的东西。请参阅链接的CREATE TABLE文档。 (我链接了最新版本,请务必找到您正在使用的Access版本。)

#1


3  

For Access, the syntax for creating a new table based on the data of a query is:

对于Access,基于查询数据创建新表的语法是:

SELECT INTO newTable
FROM oldTable;

So your code should be rewritten as:

因此,您的代码应该重写为:

String cmdstr = "insert into table " + tableName + " From Master_Sheet;";

Make sure that your SQL statement follow the MS Access syntax before you use them in your java code.

在Java代码中使用它们之前,请确保您的SQL语句遵循MS Access语法。

#2


2  

You're asking Access to create a table using this SQL statement:

您要求Access使用此SQL语句创建表:

create table yourTableName as (select * from Master_Sheet);

(including the ; at the end). It's telling you that's not valid syntax for a CREATE TABLE statement. This isn't a Java thing, it's an Access thing. See the linked CREATE TABLE documentation. (I linked the latest, be sure to find the one for the version of Access you're using.)

(包括;最后)。它告诉你这不是CREATE TABLE语句的有效语法。这不是Java的东西,它是Access的东西。请参阅链接的CREATE TABLE文档。 (我链接了最新版本,请务必找到您正在使用的Access版本。)