当前页面: 开发资料首页 → Netbeans 专题 → 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).
This demo requires NetBeans 5.0 RC1 or later.
- Download and install NetBeans 5.0.
Get the Derby Database
- Download the latest official Derby release (10.1.2.1 at the time of this writing).
- Extract the archive
- In the derby root directory (db-derby-10.1.2.1-bin), create a new folder, database.
Configure the Derby Database in NetBeans
- 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.
- Set the Derby Location to where you extracted the Derby archive (e.g., D:\db-derby-10.1.2.1-bin)
- 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
- Select Tools > Derby Database > Start Derby Server. You'll see the following in the output window:
- Select Tools > Derby Database > Create Derby Database...
- Set the Database Name to Sample. Set the User Name and Password to nbuser.
- Switch to the Runtime tab (Ctrl + 5) and expand the Databases node to see your new database
- 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
- Expand the Sample database node, right-click the Tables entry and choose Create Table...
- Give the table a name, add some columns and set their size. For example:
- Right-click the new table and choose Execute Command ...
This will open a SQL editor which we can use to add some data.
- Enter an Insert command, such as:
- Press Ctrl+Shift+E to execute the SQL commdand.
- 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.
- 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.
- Right-click the Tables node under the Derby Sample database and choose Recreate Table...
- Open the SALES_REP_TBL.grab file you saved above.
- 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):
rem -- Run Derby ij --
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:
You have now completed the NetBeans Derby Database tutorial. For more information
on working with Java GUI's in the NetBeans IDE, see: