首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache Derby: SQLSyntaxErrorException

Apache Derby: SQLSyntaxErrorException
EN

Stack Overflow用户
提问于 2012-10-05 01:46:04
回答 2查看 6.6K关注 0票数 0

请看下面的代码

代码语言:javascript
复制
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();
        }
    }
}

这个类能够以编程方式在嵌入式apache derby数据库中创建表。但是,它会给出以下错误

代码语言:javascript
复制
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

当我从表创建代码中删除"identity“关键字时,可以正常工作。但是,ID的自动生成是强制的。请帮帮我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-05 02:00:41

Derby没有identity列类型(与文档中的in the manual相同)。您需要定义一个 column。对于生成定义,Derby确实知道identity属性,但这不是数据类型。

因此,ids的列定义应该是

代码语言:javascript
复制
ids integer generated always as identity constraint pkId primary key

请注意,您也可以使用generated by default而不是always。那么,只有在插入期间没有为该列指定值的情况下,才会生成值。generated always将覆盖您提供的任何值。

票数 5
EN

Stack Overflow用户

发布于 2012-10-05 01:58:13

Apache Derby documentation指示您可以将identity关键字替换为:

代码语言:javascript
复制
  NOT NULL GENERATED ALWAYS AS IDENTITY
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12732820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档