GetStartedwithJavaDB

Get Started with Java DB in your Java Console Applications

Introduction

Java DB is Sun's supported distribution of the open source Apache Derby 100% Java technology database. It is fully transactional, secure, easy-to-use, standards-based — SQL, JDBC API, and Java EE — yet small, only 2MB. The Apache Derby project has a strong and growing community that includes developers from large companies such as Sun Microsystems and IBM as well as individual contributors.It is shipped with JDK 1.6 and NetBeans 6.

The aim of this tutorial is to get you started with using Java DB in your Java console applications using NetBeans. I recommend having the JDBC tutorial shipped with The Java Tutorial. The steps outlined there are fast, efficient and easy to follow. So lets bootstrap!

Close all the other projects that may be open in NetBeans, and then proceed.

Softwares Used

  • NetBean 6 M10
  • JDK 1.6
  • Java DB (bundled)

Start Java DB

Go to Tools->Java DB Database and Start the Java DB server File:db-start_GetStartedwithJavaDB.png

Creating the Database:

For the purpose of this tutorial, we shall create a simple database in Java DB named as "SimpleDBDemo". To create the database, go to Tools->Java DB Database->Create Database. In the dialog box, fill in the Name of the database you want, and the username/password to access the database.

File:create-db_GetStartedwithJavaDB.png

Create a Table

Now that we have a database, let us create a simple 2-column table "TABLE 1". Press "Ctrl + 5" to bring up the "Services" side-bar.

File:db-services_GetStartedwithJavaDB.png

Now, right click on the entry for "SimpleDBDemo" and choose the option for creating a new table.

File:create-table_GetStartedwithJavaDB.png

Please name the columns as is shown above, so that the subsequent Java code shown works properly.

Populate the Table

Fill in some data into the table just created using simple SQL Scripts using the "Design query" option obtained by a right-click on the table name. Eg.

INSERT INTO "DEMO"."TABLE1" values('Amit',23);
INSERT INTO "DEMO"."TABLE1" values('Ofilia',23);

So, now our Database and Table is ready. Let us now see the Java program which actually uses the Database just created.

Java Code

  • Create a New Java console application

File:new-java-app_GetStartedwithJavaDB.png

  • The main window should look similar to the following:

File:main-window_GetStartedwithJavaDB.png

  • Load the Apache Derby embedded JDBC driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  • Establish a JDBC Connection with our database, SimpleDemoDB
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/SimpleDBDemo", "demo", "demo"); // The database URL may not be same for you, lookup the "Services" side-bar.
  • Execute a SQL query against the database and display the results
Statement stmt = con.createStatement();
        
        ResultSet rs = stmt.executeQuery("SELECT * FROM DEMO.Table1");
        
        while (rs.next()) {
        String s = rs.getString("Name");
        float n = rs.getFloat("Age");
        System.out.println(s + "   " + n);
    }

You should now see all the records that you have inserted into your table. Of course, we just saw a simple SQl query, but you can go ahead and try complex ones, more suited to your requirements

  • The complete code is as follows:
/*
 * Main.java
 * 
 * Created on 17 Sep, 2007, 10:49:17 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javadbdemo;
import java.sql.*;

/**
 *
 * @author amit
 */
public class Main {

    /** Creates a new instance of Main */
    public Main() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try{ 
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        }catch(ClassNotFoundException e){
            System.out.println(e);
        }
        
        try{
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/SimpleDBDemo", "demo", "demo");
        Statement stmt = con.createStatement();
        
        ResultSet rs = stmt.executeQuery("SELECT * FROM DEMO.Table1");
        
        while (rs.next()) {
        String s = rs.getString("Name");
        float n = rs.getFloat("Age");
        System.out.println(s + "   " + n);
    }
        }catch(SQLException e){
            System.err.println(e);
        }                 
     
      }

}

Conclusion

This tutorial showed you how easy it is to use Java DB in your applications. For further information on Java DB and related topics consult the section below.

Links


--ByAmit Kumar Saha

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo