Java Apache Derby:SQLSyntaxErrorException

Java Apache Derby:SQLSyntaxErrorException,java,sql,jdbc,derby,Java,Sql,Jdbc,Derby,请看一下下面的代码 package normal; //This class if s for checking the database. If the database doesn't exists, this class will create one import java.sql.*; public class DatabaseCheck { private Connection con; public DatabaseCheck() {

请看一下下面的代码

package normal;

//This class if s for checking the database. If the database doesn't exists, this class will create one

import java.sql.*;

public class DatabaseCheck
{
    private Connection con;

    public DatabaseCheck()
    {
        createConnection();
        try
        {
            Statement st = con.createStatement();
            st.executeQuery("select * from PhoneData");
        }
        catch(Exception e)
        {
            System.out.println(e.getLocalizedMessage());

            if(e.getLocalizedMessage().equals("Schema 'SA' does not exist"))
            {
                try
                {
                PreparedStatement ps = con.prepareStatement("create table PhoneData(ids int identity constraint pkId primary key,names varchar(20),mobileNumber1 varchar(20),mobileNumber2 varchar(20),landNumber1 varchar(20),landNumber2 varchar(20),address varchar(100),category varchar(20),nickName varchar(20),email varchar(20),middleName varchar(20),lastName varchar(20),city varchar(20),country varchar(20))");
                ps.execute();

                PreparedStatement ps2 = con.prepareStatement("create table Emails(accountType varchar(10) constraint pk_user primary key,userName varchar(50) ,passwords varchar(50))");
                ps2.execute();

                }
                catch(Exception e2)
                {
                    e2.printStackTrace();
                }
            }
        }
        finally
        {
            closeConnection();
        }
    }

     public void createConnection()
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

            con = DriverManager.getConnection("jdbc:derby:PhoneBook;create=true","sa","sasasa");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

      public void closeConnection()
    {
        try
        {
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
此类能够以编程方式在嵌入式ApacheDerby数据库中创建表。但是,它给出了以下错误

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "identity" at line 1, column 32.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
    at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at normal.DatabaseCheck.<init>(DatabaseCheck.java:27)
    at normal.MyPhoneBookApp.main(MyPhoneBookApp.java:25)
Caused by: java.sql.SQLException: Syntax error: Encountered "identity" at line 1, column 32.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 15 more
Caused by: ERROR 42X01: Syntax error: Encountered "identity" at line 1, column 32.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
    at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
    ... 9 more
java.sql.SQLSyntaxErrorException:语法错误:在第1行第32列遇到“identity”。
位于org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(未知源)
位于org.apache.derby.impl.jdbc.Util.generateCsSQLException(未知源)
位于org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(未知源)
位于org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(未知源)
位于org.apache.derby.impl.jdbc.EmbedConnection.handleException(未知源)
位于org.apache.derby.impl.jdbc.ConnectionChild.handleException(未知源)
位于org.apache.derby.impl.jdbc.EmbeddePreparedStatement。(未知源)
位于org.apache.derby.impl.jdbc.EmbeddePreparedStatement20。(未知来源)
位于org.apache.derby.impl.jdbc.EmbeddePreparedStatement30。(未知来源)
位于org.apache.derby.impl.jdbc.EmbeddePreparedStatement40。(未知来源)
位于org.apache.derby.jdbc.Driver40.newEmbeddePreparedStatement(未知源)
在org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(未知源代码)
在org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(未知源代码)
DatabaseCheck.(DatabaseCheck.java:27)
在normal.MyPhoneBookApp.main上(MyPhoneBookApp.java:25)
原因:java.sql.SQLException:语法错误:在第1行第32列遇到“identity”。
位于org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(未知源)
位于org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapargsforttransportacrossdrda(未知源)
... 还有15个
原因:错误42X01:语法错误:在第1行第32列遇到“identity”。
位于org.apache.derby.iapi.error.StandardException.newException(未知源)
位于org.apache.derby.impl.sql.compile.ParserImpl.ParserStatement(未知源)
位于org.apache.derby.impl.sql.GenericStatement.prepMinion(未知源)
位于org.apache.derby.impl.sql.GenericStatement.prepare(未知源)
位于org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(未知源)
... 9更多
当我从表创建代码中删除“identity”关键字时,这就可以了。但是,自动生成ID是必需的。请帮忙

表示您可以将
标识
关键字替换为:

  NOT NULL GENERATED ALWAYS AS IDENTITY
指示您可以将
标识
关键字替换为:

  NOT NULL GENERATED ALWAYS AS IDENTITY

Derby没有
标识
列类型(如文档所示)。您需要定义一个。对于生成定义,Derby确实知道
identity
属性,但这不是数据类型

因此,
ids
的列定义应该是

ids integer generated always as identity constraint pkId primary key

请注意,您也可以使用默认生成的
,而不是始终使用
。然后,只有在插入期间未为该列指定值时,才会生成值<代码>始终生成将覆盖您提供的任何值。

Derby没有
标识
列类型(如文档所示)。您需要定义一个。对于生成定义,Derby确实知道
identity
属性,但这不是数据类型

因此,
ids
的列定义应该是

ids integer generated always as identity constraint pkId primary key

请注意,您也可以使用默认生成的
,而不是始终使用
。然后,只有在插入期间未为该列指定值时,才会生成值<代码>始终生成将覆盖您提供的任何值。

请尝试此处描述的语法。试试这里描述的语法。谢谢你!我真的很感激!谢谢你!我真的很感激!