Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error while creating table from another table

Tags:

java

ms-access

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)

Please help, i am very new to java coding.

like image 664
Raju Avatar asked Dec 27 '25 16:12

Raju


1 Answers

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

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.

like image 113
Renaud Bompuis Avatar answered Dec 31 '25 14:12

Renaud Bompuis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!