站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题Derby Tutorial

Derby Tutorial

摘要: Derby is the open source database being developed as part of the Apache DB Project. The Derby project does not develop any GUI tooling. Derby support is included in NetBeans 5.0 (starting with the RC1 release).

Tutorial Prep

This demo requires NetBeans 5.0 RC1 or later.
  1. Download and install NetBeans 5.0.

Tutorial

Get the Derby Database

  1. Download 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 > Options (NetBeans > Preferences on the Macintosh) and click the Advanced Options button. Expand the IDE Configuration > 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.

Start the Derby Server and Create a Database

  1. Select Tools > Derby Database > Start Derby Server. You'll see the following in the output window:


  2. Select Tools > Derby Database > Create Derby Database...
  3. Set the Database Name to Sample. Set the User Name and Password to nbuser.


  4. Switch to the Runtime tab (Ctrl + 5) and expand the Databases node to see your new database


  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...


  2. Give the table a name, add some columns and set their size. For example:


  3. Right-click the new table and choose Execute Command ...



    This will open a SQL editor which we can use to add some data.
  4. Enter an Insert command, such as:


  5. Press Ctrl+Shift+E to execute the SQL commdand.


  6. Then right-click the table again and choose View Data.... You'll see the select SQL command and the result:



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. See the J2EE Quick Start Guide 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.


  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.


  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:

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 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 will create three tables and populate them with data.

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:

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

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:

top


↑返回目录
前一篇: Deploy and Run Java ME Applications using Sun Java Application Server
后一篇: Developer Collaboration Module Quickstart