31

I'm coming from MySQL world, please help.

Is it possible to create autoincrement key from NetBeans IDE in JavaDB?

Do you use some more advanced db clients, which?

Thanks.

umpirsky
  • 9,902
  • 13
  • 71
  • 96

7 Answers7

69

This may help you:

CREATE TABLE "custinf"

(    
   "CUST_ID" INT not null primary key
        GENERATED ALWAYS AS IDENTITY
        (START WITH 1, INCREMENT BY 1),   
   "FNAME" VARCHAR(50),     
   "LNAME" VARCHAR(50),
   "ADDR" VARCHAR(100),
   "SUBURB" VARCHAR(20),
   "PCODE" INTEGER,  
   "PHONE" INTEGER,
   "MOB" INTEGER,    
   "EMAIL" VARCHAR(100),
   "COMM" VARCHAR(450)    
);

That's how i got mine to work... to ages to get the frigging thing to actually understand me but that's the nature of code :D

BTW!- There is a way to do it in the ide interface goto the services window, expand your connection, expand your projects name, expand tables, right click indexes and select add index... the rest of the process speaks for itself really...

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Sam
  • 691
  • 1
  • 5
  • 2
  • Worked for me, thanks! I don't know why they decided to write everything out in English like that. I was trying 'IDENTITY (1,1)', which is used elsewhere, but didn't think there would be all that extra crap around it. – Lemmings19 Apr 27 '12 at 01:02
  • 1
    Great stuff. It only worked for me when I kept the column names **UPPERCASE**. – Yster Dec 16 '15 at 16:09
  • Why is this not the selected answer? :C – David Villamizar Oct 12 '17 at 21:48
13

Found a way of setting auto increment in netbeans 8.0.1 here on StackoOverflow Screenshot below:

see screenshot here

Community
  • 1
  • 1
user28864
  • 3,375
  • 1
  • 25
  • 19
12

If you look at this url: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/

this part of the schema may be what you are looking for.

 ID          INTEGER NOT NULL 
                PRIMARY KEY GENERATED ALWAYS AS IDENTITY 
                (START WITH 1, INCREMENT BY 1),
James Black
  • 41,583
  • 10
  • 86
  • 166
  • 1
    Thanks. Yes, but I don't need an IDE in order to do that. If I use IDE and it's gui tools, I want to be able to do that from the interface. If I end up creationg tables from plain SQL, then why am I using netbeans? I'm still beginner with derby syntax :P – umpirsky Jul 26 '10 at 13:12
  • But, I was just showing that you can create auto-generated keys for JavaDB, but as you mentioned you will be creating the schema yourself, in SQL. – James Black Jul 26 '10 at 13:34
  • Thanks. But I knew that, I just can't see the way to do it from netbeans. Will try with alter table... – umpirsky Jul 27 '10 at 08:59
2

I couldn't get the accepted answer to work using the Netbeans IDE "Create Table" GUI, and I'm on Netbeans 8.2. To get it to working, create the id column with the following options e.g.

enter image description here

and then use 'New Entity Classes from Database' option to generate the entity for the table (I created a simple table called PERSON with an ID column created exactly as above and a NAME column which is simple varchar(255) column). These generated entities leave it to the user to add the auto generated id mechanism.

GENERATION.AUTO seems to try and use sequences which Derby doesn't seem to like (error stating failed to generate sequence/sequence does not exist), GENERATION.SEQUENCE therefore doesn't work either, GENERATION.IDENTITY doesn't work (get error stating ID is null), so that leaves GENERATION.TABLE.

Set your persistence unit's 'Table Generation Strategy' button to Create. This will create tables that don't exist in the DB when your jar is run (loaded?) i.e. the table your PU needs to create in order to store ID increments. In your entity replace the generated annotations above your id field with the following...

enter image description here

I also created a controller for my entity class using 'JPA Controller Classes from Entity Classes' option. I then create a simple main class to test the id was auto generated i.e.

enter image description here

The result is that the PERSON_ID_TABLE is generated correctly and my PERSON table has two PERSON entries in it with correct, auto generated ids.

Alan Smith
  • 1,069
  • 4
  • 19
  • 23
1

It's not possible right now, on Netbeans 7.0.1 . The GUI tool to create columns on a tables is very limited and does not exist a plugin that offer that feature.

Esteban Cacavelos
  • 753
  • 10
  • 22
  • are you really sure about that? I did it 3 months ago and today i forgot how i did it lolz – Nitesh Verma Apr 05 '13 at 13:53
  • You did it with 7.0.1 version?. If you did would be useful you answer the question explaining how you did it. I guess this can be done with newer versions of Netbeans. – Esteban Cacavelos Apr 09 '13 at 13:04
  • 1
    He does it here: https://stackoverflow.com/questions/14316187/alter-a-table-column-with-auto-increment-by-1-in-derby I don't know if it is Derby specific though. – bulltorious Sep 19 '14 at 14:18
0

If you want to use Netbeans to define tables read this https://codezone4.wordpress.com/2012/06/19/java-database-application-using-javadb-part-1/ Simply define column as integer and create database, then grab structure to a temporary file, then delete table. Right clik to tables folder and select recreate table, select saved file and edit script for auto increment.

UJK
  • 89
  • 1
  • 3
0
  1. Add a new column in the table using the interface
  2. Write the name of column and fill other information as well
  3. In check field, don't uncheck it and write "INCREMENT BY 1" in it.

Voila!!

bensiu
  • 24,660
  • 56
  • 77
  • 117