Last Updated:

What is JavaDB?

JavaDB is a very simple and lightweight DBMS that requires neither administration nor special fine-tuning. However, for all its simplicity, this is quite a decent relational DBMS, which very often greatly facilitates the life of a Java developer.


It is often necessary to temporarily "reset" structured files to disk. It is not really possible to develop algorithms for saving/loading such structures to disk. At the same time, the use of even MySQL looks like "shooting a cannon at sparrows", in such cases JavaDB is an excellent solution.

JavaDB is included with the JDK and is installed with it by default. Below I will give a sequence of the very first and simplest actions...

 

By default, JavaDB is installed in the JDK directory, I have it here on Windows7:

  C:\Program Files\Java\jdk1.7.0_05\db

 

First of all, we set the environment variable DERBY_HOME

  DERBY_HOME "C:\Program Files\Java\jdk1.7.0_05\db"

 

After installing the DERBY_HOME, you can run the "DBMS Browser", a kind of analogue of sqlplus for Oracle.
The documentation says it is as follows:

  ij

I got nothing but a mistake: I could not defeat it on the move, who knows what the matter is.
The documentation describes another way, which is what worked for me: Of course, the path to Java must be spelled in the Path variable, or you need to set the full path.

  Files\Java\jdk1.7.0_05\db""=="" was unexpected at this time.
  java -jar %DERBY_HOME%\lib\derbyrun.jar ij

 

In the running "DBMS browser", and this is essentially a command line (similar in appearance to sqlplus Oracle), you can create a new database with the following command:

  CONNECT 'jdbc:derby:firstdb;create=true';

It's funny, in JavaDB there is no special command "CREATE DATABASE", if there is no database at the time of connection, it will create itself, for this you need the parameter create=true.


As a result of CONNECT running on the hard disk, the firstdb directory will be created in the current directory, in which the database will lie.

 

The DBMS is ready to go, let's create a trial table:

create table SUPPLIERS
  (SUP_ID integer NOT NULL,
  SUP_NAME varchar(40) NOT NULL,
  STREET varchar(40) NOT NULL,
  CITY varchar(20) NOT NULL,
  STATE char(2) NOT NULL,
  ZIP char(5),
  PRIMARY KEY (SUP_ID));

And now let's fill it out: And now let's see what happened:

insert into SUPPLIERS values(49,  'Superior Coffee', '1 Party Place', 'Mendocino', 'CA', '95460');
insert into SUPPLIERS values(101, 'Acme, Inc.', '99 Market Street', 'Groundsville', 'CA', '95199');
insert into SUPPLIERS values(150, 'The High Ground', '100 Coffee Lane', 'Meadows', 'CA', '93966');
insert into SUPPLIERS values(456, 'Restaurant Supplies, Inc.', '200 Magnolia Street', 'Meadows', 'CA', '93966');
insert into SUPPLIERS values(927, 'Professional Kitchen', '300 Daisy Avenue', 'Groundsville', 'CA', '95199');
  select * from SUPPLIERS;

JavaDB can be easily created directly from the application and does not bother the end user with additional installation complexities, setting up authority, importing scripts, etc.

 

CREATING A DATABASE WITH JAVADB IN NETBEANS

 

In this first tutorial, I will share how to create a Database directly from NetBeans with JavaDB already available in NetBeans IDE 8.2, in other words we do not utilize MySQL Database creation software such as XAMPP, etc.

Okee, let's get started.

 

  1. Open the NetBeans IDE 8.2.
  2. We first close all existing projects.
  3. Then go to the Services Tab in the Windows Group Menu.
JAVADB IN NETBEANS

 

  1. Next we will start the database server by clicking node (+) inDatabases, there is a JavaDB sub-menu right-click the sub-menu then select Start Server.
JAVADB IN NETBEANS 2

 

  1. If it is already in Start, an output will appear indicating thatJavaDB is active.
  2. We have successfully turned on its JavaDB server and the server is ready to serve the host / user requests.
  3. Now we will create the Database by right-clicking again on theJavaDB sub-menu then select Create Database.
JAVADB IN NETBEANS 3

 

  1. Then a dialog box will appear like this:
JAVADB IN NETBEANS 4

 

  1. After that fill everything like:

Database Name                       

:  MyDB

User Name                                

:  mydb

Password

:  mydb

Confirm Password

:  mydb

 

JAVADB IN NETBEANS 5

 

  1. The database has been created, it will be visible in theJavaDB list.
JAVADB IN NETBEANS 6

 

  1. Next we will create a Table from the Database with the nameMyDB that we created earlier.
  2. First we will connect the Database by right-clicking theMyDB Database Connection then select Connect, it serves to activate the Apache Derby Network Server on NetBeans.
JAVADB IN NETBEANS 7

 

  1. After that, a list ofMyDB Schemas will appear and also other schemas provided by the NetBeans server.
JAVADB IN NETBEANS 8

 

  1. Click the node (+) onMyDB will appear several lists, right-click on Tables and then select Create Table.
JAVADB IN NETBEANS 9

  1. Then a dialog box will appear like this:
JAVADB IN NETBEANS 10
  1. Then we will create a table with the nameMyTable containing the ID Column, Name, Jenis_Kelamin, Job.
  2. In theTable Name box change Untitled to MyTable.
  3. To add its columns clickAdd Column, then a dialog city will appear like this:
JAVADB IN NETBEANS 11
  1. For the first column we will create anID column which is the Primary Key intending that the contents in the ID column must be Unique and nothing should be the same.
  2. Change everything to as below, if you have clickedOK.

 

JAVADB IN NETBEANS 12

 

  1. After that we will create aName column, click add column again and then fill in like this:
JAVADB IN NETBEANS 13

 

  1. do the same to add theGender column and also Occupation, so that the total becomes like this:
JAVADB IN NETBEANS 14

 

  1. If so next clickOK.
  2. The table with the nameMyTable and with the columns ID, Name, Jenis_Kelamin, The work is done.
JAVADB IN NETBEANS

 

  1. Okeeedehh next we will add records from each column by right-clicking and then selectingView Data in the MyTable table.
JAVADB IN NETBEANS 16

 

  1. Then the SQL Command view will appear and below it there is a table view that will display all the commands entered in the SQL Command.

 

JAVADB IN NETBEANS 17
  1. Fortunately in JavaDB NetBeasn this if we want to add data or records do not need to type in SQL syntax just need to click theInsert Record button.
JAVADB IN NETBEANS 18

 

  1. Click theInsert Record button and then a dialog box will appear like this:

 

JAVADB IN NETBEANS 19
  1. Enter the data like this:
JAVADB IN NETBEANS 20

 

  1. If you have clickedOK.
  2. Your table appears with the data that has been inputted earlier.

 

JAVADB IN NETBEANS 21
  1. Until here we have succeeded in creating a Database with tables, columns and also the data.
  2. There are several SQL Syntax that you can try in the Source SQL Command, such as:

-          SELECT * FROM "MYDB". MYTABLE WHERE JOB = 'Student'

-          Select * from mytable where job='student'

JAVADB IN NETBEANS 22