---
title: "NetBeans Derby Tutorial "
author: "ssgemail"
date: "2007-03-12T15:24:08Z"
tags: []
slug: "netbeans-derby-tutorial"
draft: false
lastmod: "2007-03-12T15:24:08Z"
id: 59277
url: "https://tim1995.iteye.com/blog/59277"
---

# Derby Tutorial

## Tutorial Prep

This demo requires NetBeans 5.0 RC1 or later.

1. Download and install [NetBeans 5.0](http://www.netbeans.org/community/releases/50/).

## Tutorial

### Get the Derby Database

1. [Download](http://db.apache.org/derby/derby_downloads.html) the latest official Derby release (10.1.2.1 at the time of this writing).
2. Extract the archive
3. In the derby root directory (db-derby-10.1.2.1-bin), create a new folder, **database**.

### Configure the Derby Database in NetBeans

1. Select Tools &gt; Options (NetBeans &gt; Preferences on the Macintosh) and click the Advanced Options button. Expand the IDE Configuration &gt; Server and External Tools Settings nodes and select the Derby Database node.
2. Set the Derby Location to where you extracted the Derby archive (e.g., D:\\db-derby-10.1.2.1-bin)
3. Set the Database Location to the location of the database folder created above (e.g., D:\\db-derby-10.1.2.1-bin\\database) and close the Options dialog.
   
   [![](http://www.netbeans.org/images/articles/derby/small_advanced-options.png "click to enlarge")](http://www.netbeans.org/images/articles/derby/advanced-options.png)

### Start the Derby Server and Create a Database

1. Select **Tools &gt; Derby Database &gt; Start Derby Server**. You'll see the following in the output window:
   
   ![](http://www.netbeans.org/images/articles/derby/output-server-ready.png)
2. Select **Tools &gt; Derby Database &gt; Create Derby Database...**
3. Set the Database Name to **Sample**. Set the User Name and Password to **nbuser**.
   
   ![](http://www.netbeans.org/images/articles/derby/create-derby-database.png)
4. Switch to the Runtime tab (Ctrl + 5) and expand the Databases node to see your new database
   
   ![](http://www.netbeans.org/images/articles/derby/runtime-databases.png)
5. Right-click the Sample database and choose connect.

### Adding a Table to the Database

Obviously, since the database is brand new, it's empty. Let's explore the options for adding tables and data.

##### [Create Table Wizard]()

[]()

1. [Expand the Sample database node, right-click the Tables entry and choose **Create Table...**  
   \
   ![](http://www.netbeans.org/images/articles/derby/create-table-menu.png)]()
2. [Give the table a name, add some columns and set their size. For example:]()
   
   [![](http://www.netbeans.org/images/articles/derby/small_create-table.png "click to enlarge")](http://www.netbeans.org/images/articles/derby/create-table.png)
3. Right-click the new table and choose **Execute Command ...**
   
   ![](http://www.netbeans.org/images/articles/derby/execute-command.png)
   
   This will open a SQL editor which we can use to add some data.
4. Enter an Insert command, such as:
   
   [![](http://www.netbeans.org/images/articles/derby/small_insert-command.png "click to enlarge")](http://www.netbeans.org/images/articles/derby/insert-command.png)
5. Press Ctrl+Shift+E to execute the SQL commdand.
   
   [![](http://www.netbeans.org/images/articles/derby/small_insert-command-output.png "click to enlarge")](http://www.netbeans.org/images/articles/derby/insert-command-output.png)
6. Then right-click the table again and choose **View Data...**. You'll see the select SQL command and the result:
   
   [![](http://www.netbeans.org/images/articles/derby/small_viewdata1.png "click to enlarge")](http://www.netbeans.org/images/articles/derby/viewdata1.png)

##### Recreate Table Wizard

This wizard's handy if you have a table in another database that you'd like to recreate in Derby. For this example, I'm using the Pointbase database that comes with the [J2EE 1.4 SDK](http://java.sun.com/j2ee/1.4/download.html#sdk). See the [J2EE Quick Start Guide](http://www.netbeans.org/kb/41/quickstart-j2ee.html) for information on configuring the J2EE 1.4 SDK with NetBeans.

1. Connect to the Pointbase Sample database (the password is pbpublic). Expand the Tables node. Right-click the SALES\_REP\_TBL and choose **Grab Structure...**. Then Save the grab file to some location on disk.
   
   ![](http://www.netbeans.org/images/articles/derby/grab-structure.png)
2. Right-click the **Tables** node under the Derby Sample database and choose **Recreate Table...**
3. Open the SALES\_REP\_TBL.grab file you saved above.
   
   ![](http://www.netbeans.org/images/articles/derby/recreate-table.png)
4. At this point you can change the table name or edit the create script. For our exercise, just click **OK** to create the table and you'll see the new table under the **Tables** node:
   
   ![](http://www.netbeans.org/images/articles/derby/recreate-table-result.png)

##### Using a SQL Script

Probably the most popular way to manage your database. Unfortunately, the SQL editor in NetBeans only accepts a single command at a time. [Issue 68844](http://www.netbeans.org/issues/show_bug.cgi?id=68844) has been opened to address this limitation.

So our alternative is to use the *ij* tool that comes with the Derby database. The easiest way to work with this tool is to create a wrapper script. For example, on Windows, I created the following ij.bat file in my Windows directory (so it would automatically be on the path):

```
  @echo off
  rem -- Run Derby ij --
  set LIBPATH=D:\db-derby-10.1.2.1-bin\lib
  java -classpath "%LIBPATH%\derbytools.jar;%LIBPATH%\derby.jar;%LIBPATH%\derbyclient.jar" org.apache.derby.tools.ij %1
  
```

Now, from anywhere on the system, I can call *ij* against a sql script. For example, [Customer.sql](http://testwww.netbeans.org/files/documents/71/628/Customers.sql) will create three tables and populate them with data.

[![](http://www.netbeans.org/images/articles/derby/small_ij.png)](http://www.netbeans.org/images/articles/derby/ij.png)

Now return to NetBeans, right-click the Tables entry and choose **Refresh** to see the 3 new tables. Right-click the **CUSTOMER\_TBL** and choose **View Data...** to see the table contents. NetBeans puts the SQL statement in the SQL editor and executes it, producing the following results:

[![](http://www.netbeans.org/images/articles/derby/small_viewdata.png "click to enlarge")](http://www.netbeans.org/images/articles/derby/viewdata.png)

You can use the SQL Editor to execute a different SQL statement. For example:

[![](http://www.netbeans.org/images/articles/derby/small_count.png "click to enlarge")](http://www.netbeans.org/images/articles/derby/count.png)

[**Next Steps**  
\
You have now completed the NetBeans Derby Database tutorial. For more information on working with Java GUI's in the NetBeans IDE, see:]()

- []()
- []()**[Connecting a GUI to a Derby Database with NetBeans IDE](http://www.netbeans.org/kb/50/gui-database.html).** A guide through the process of connecting an application GUI to a Derby database.
- **[GUI Building in the NetBeans IDE](http://www.netbeans.org/kb/50/quickstart-gui.html).** A quick guide through the process of creating graphical user interfaces with the NetBeans IDE.
- **[Matisse GUI Builder FAQ](http://wiki.netbeans.info/wiki/view/NetBeansUserFAQ#section-NetBeansUserFAQ-GUIEditorMatisse).** A collection of useful tips and tricks for using NetBeans GUI Builder.
- **[NetBeans Support and Docs](http://www.netbeans.org/kb/).** Full list of articles, FAQs, and mailing lists for NetBeans IDE users.