Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Derby: Run a JDBC program using the embedded driver

In my previous posts, I explained how to create database and tables using both embedded mode and client-server mode.

Let’s try to connect to the embedded Derby driver using jdbc. JDBC stands for Java Database Connectivity, and is a platform independent interface between databases and Java. Just like packages awt, swing, and lang, jdbc is also a package, that allows Java programs to access database management systems like MySql, oracle, db2 etc.,

If you are new to jdbc, I refer you go through my below tutorial.
http://self-learning-java-tutorial.blogspot.in/2014/10/jdbc.html

Below step by step procedure explains simple jdbc program that creates Employee table, insert 3 records into it and display the employee table.

Step 1: Open Eclipse, create new maven project.

Right click on Eclipse ‘Project Explorer’ -> New -> Other.

Select ‘Maven Project’ and press next.

Select the check box ‘Create a simple project (skip archetype selection)’.

Press Next.

Give the group id and project id as 'jdbc_helloworld'.

Click on the button Finish.

It creates project structure like below.

Step 2: Add derby jdbc dependency to pom.xml.

pom.xml

xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
jdbc_helloworld
jdbc_helloworld
0.0.1-SNAPSHOT




org.apache.derby
derby
10.13.1.1





Note
If you got any error like 'java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver' it may be because the scope of the dependency is set to test.

                
                
                          org.apache.derby
                          derby
                          10.13.1.1
                          test
                
                
You need to remove the element from the dependency and update the dependency like below.

                
                
                          org.apache.derby
                          derby
                          10.13.1.1
                

Step 3: Create a package com.sample.

Define class ‘Test’ under the package ‘com.sample’.


Test.java

package com.sample;

/* Step 1: Import sql package */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Test {

private static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private static String dbName = "organization";
private static String connectionURL = "jdbc:derby:" + dbName + ";create=true";

public static void main(String args[]) throws SQLException, ClassNotFoundException {
/* Step 2: Load Driver */
System.out.println("Loading/Registering driver");
Class.forName(driver);

/* Step 3: Open connection to database */
System.out.println("Connecting to database");
Connection conn = DriverManager.getConnection(connectionURL);

/* Create table employee */
String query = "CREATE TABLE employee (id int, name varchar(30), PRIMARY KEY(id))";
Statement stmt = conn.createStatement();
stmt.execute(query);

/* Insert data to employee table */
query = "INSERT INTO employee values(1, 'Krishna')";
stmt.execute(query);
query = "INSERT INTO employee values(2, 'Arjun')";
stmt.execute(query);

query = "SELECT * FROM employee";
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + " " + name);
}
rs.close();
conn.close();
}
}

Output

Loading/Registering driver
Connecting to database
1 Krishna
2 Arjun


Load Derby Driver
Below statements are used to load derby embedded driver.
private Static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver);

Connect to the embedded server
Below statements are used to connect to the embedded server.
private static String dbName = "organization";
private static String connectionURL = "jdbc:derby:" + dbName + ";create=true";
Connection conn = DriverManager.getConnection(connectionURL);

Create table employee
String query = "CREATE TABLE employee (id int, name varchar(30), PRIMARY KEY(id))";
Statement stmt = conn.createStatement();
stmt.execute(query);

Insert data into employee table
String query = "INSERT INTO employee values(1, 'Krishna')";
stmt.execute(query);
query = "INSERT INTO employee values(2, 'Arjun')";
stmt.execute(query);

Display the records in employee table
query = "SELECT * FROM employee";
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
         int id = rs.getInt("id");
         String name = rs.getString("name");
         System.out.println(id + " " + name);
}

Close result set and connection
rs.close();
conn.close();


Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Derby: Run a JDBC program using the embedded driver

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×