2012-10-04 2 views
0

S'il vous plaît jeter un oeil sur le code suivantApache Derby: SQLSyntaxErrorException

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(); 
     } 
    } 
} 

Cette classe est capable de créer des tables dans la base de données par programmation derby apache intégré. Mais, il donne l'erreur suivante

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 

Lorsque je supprime le mot-clé « identité » de la table code de création, ce beau travail. Mais, la génération automatique d'ID est obligatoire. S'il vous plaît aider!

+0

Essayez la syntaxe décrite ici. http://db.apache.org/derby/docs/10.0/manuals/develop/develop132.html – Scott

Répondre

5

Derby n'a pas de type colonne identity (comme documenté in the manual). Vous devez définir un generated column. Pour la définition de génération, Derby connaît en effet un attribut identity, mais ce n'est pas un type de données.

Ainsi, la définition de la colonne pour ids devrait être

ids integer generated always as identity constraint pkId primary key 

Notez que vous pouvez également utiliser generated by default au lieu de always. Ensuite, une valeur ne sera générée que si vous ne spécifiez pas de valeur pour cette colonne pendant l'insertion. generated always remplacera toute valeur que vous fournissez.

+0

Merci à vous! J'apprécie vraiment cela! –

Questions connexes