Thursday, 29 June 2017

Derby: Hello World Application

In this post, I am going to show, how to create a database, table and insert records into the table.

Step 1: Create a directory 'derby_samples'.

Step 2: Open command prompt/terminal, navigate to the directory 'derby_samples'  and type the command ‘ij’.

C:\Users\Krishna\Documents\Study\Apache Derby\examples\derby_samples>ij
ij version 10.13
ij>

ij command is located in 'DERBY_HOME/bin' directory. Make sure  'DERBY_HOME/bin' is added to your system path.

ij tool is the client application that connects to Derby server.

Step 2: Create and connect to the database ‘organization’.

Below statement is used to create and connect to the database ‘organization’.

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

Below table summarizes the tokens of above command.
Token
Description
CONNECT
CONNECT command is used Used to establish a connection to a database.
jdbc:derby
The JDBC protocol specification for the Derby driver.
organization
The name of the database. Since no filepath is specified, the database is created in the default working directory 'derby_samples'
;create=true
Create attribute is used to create a database. Derby does not have an SQL ‘create database’ command.
;
The semicolon is the ij command terminator.

In ij prompt type below command.


CONNECT 'jdbc:derby:organization;create=true';
Once the command is executed successfully, you can able to see ‘derby.log’ and organization directory in ‘derby_samples’ directory.

Step 3: Create a table employee.

Syntax
CREATE TABLE table_name(
  column1 data_type,
  column2 data_type,
         ...
         ...
  columnn data_type,
  PRIMARY KEY(columns)
);

Use below command to create table employee.

CREATE TABLE employee (id INT, name VARCHAR(20));

ij> CREATE TABLE employee (id INT, name VARCHAR(20));
0 rows inserted/updated/deleted


Step 4: Insert data into the table ‘employee’.

Use below commands to insert records to the table ‘employee’.

INSERT INTO employee VALUES (1, 'Hari Krishna');
INSERT INTO employee VALUES (2, 'Gopi Battu');
INSERT INTO employee VALUES (3, 'Chamu Majety');

ij> INSERT INTO employee VALUES (1, 'Hari Krishna');
1 row inserted/updated/deleted
ij> INSERT INTO employee VALUES (2, 'Gopi Battu');
1 row inserted/updated/deleted
ij> INSERT INTO employee VALUES (3, 'Chamu Majety');
1 row inserted/updated/deleted

Step 5: Display the records from the table ‘employee’.

ij> SELECT * FROM employee;
ID         |NAME
--------------------------------
1          |Hari Krishna
2          |Gopi Battu
3          |Chamu Majety


Step 6: Exit from Derby by typing exit; command.



Previous                                                 Next                                                 Home

No comments:

Post a Comment