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:
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:
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:
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.
- Open the NetBeans IDE 8.2.
- We first close all existing projects.
- Then go to the Services Tab in the Windows Group Menu.
- 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.
- If it is already in Start, an output will appear indicating thatJavaDB is active.
- We have successfully turned on its JavaDB server and the server is ready to serve the host / user requests.
- Now we will create the Database by right-clicking again on theJavaDB sub-menu then select Create Database.
- Then a dialog box will appear like this:
- After that fill everything like:
- The database has been created, it will be visible in theJavaDB list.
- Next we will create a Table from the Database with the nameMyDB that we created earlier.
- 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.
- After that, a list ofMyDB Schemas will appear and also other schemas provided by the NetBeans server.
- Click the node (+) onMyDB will appear several lists, right-click on Tables and then select Create Table.
- Then a dialog box will appear like this:
- Then we will create a table with the nameMyTable containing the ID Column, Name, Jenis_Kelamin, Job.
- In theTable Name box change Untitled to MyTable.
- To add its columns clickAdd Column, then a dialog city will appear like this:
- 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.
- Change everything to as below, if you have clickedOK.
- After that we will create aName column, click add column again and then fill in like this:
- do the same to add theGender column and also Occupation, so that the total becomes like this:
- If so next clickOK.
- The table with the nameMyTable and with the columns ID, Name, Jenis_Kelamin, The work is done.
- Okeeedehh next we will add records from each column by right-clicking and then selectingView Data in the MyTable table.
- 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.
- 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.
- Click theInsert Record button and then a dialog box will appear like this:
- Enter the data like this:
- If you have clickedOK.
- Your table appears with the data that has been inputted earlier.
- Until here we have succeeded in creating a Database with tables, columns and also the data.
- 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'