This document demonstrates how to set up a connection to Java DB database in NetBeans IDE. Once a connection is made, you can begin working with the database in the IDE, allowing you to create tables, populate them with data, run SQL statements and queries, and more.
![]()
The Java DB database is Sun's supported distribution of Apache Derby. Java DB is a fully transactional, secure, standards-based database server, written entirely in Java, and fully supports SQL, JDBC API, and Java EE technology. The Java DB database is packaged with the GlassFish application server, and is included in JDK 6 as well. For more information on Java DB database, consult the official documentation.
Contents
To follow this tutorial, you need the following software and resources.
Vendor: Apache Software Foundation. Artifact: derbyclient. Name: Derby Network Client. Description: URL: Download: MAVEN. Home » org.apache.derby » derbyclient Apache Derby Client JDBC Driver The Derby client JDBC driver, used to connect to a Derby server over a network connection.
Note.
Configuring the Database
If you have the GlassFish Server registered in your NetBeans IDE installation, Java DB will already be registered for you. Therefore, you can skip ahead to Starting the Server and Creating a Database.
If you downloaded the GlassFish server separately and need help registering it in NetBeans IDE, see Registering a GlassFish Server Instance in the IDE's Help Contents (F1).
If you just downloaded Java DB on its own, perform the following steps.
Before continuing further, it is important to understand the components found in Java DB's root directory:
Registering the Database in NetBeans IDE
Now that the database is configured, perform the following steps to register Java DB in the IDE.
Starting the Server and Creating a Database
The Java DB Database menu options are displayed when you right-click the Java DB node in the Services window. This contextual menu items allow you to start and stop the database server, create a new database instance, as well as register database servers in the IDE (as demonstrated in the previous step). To start the database server:
After you create the database, if you expand the Databases node in the Services window you can see that the IDE created a database connection and that the database was added to the list under the Java DB node.
Connecting to the Database
So far, you have successfully started the the database server and created a database instance named contact in the IDE. In the Services window of the IDE you can perform the following common tasks on database structures.
In order to begin working with the contact database, you need to create a connection to it. To connect to the contact database perform the following steps.
Creating Tables
The contact database that you just created is currently empty. It does not yet contain any tables or data. In NetBeans IDE you can add a database table by either using the Create Table dialog, or by inputting an SQL statement and running it directly from the SQL Editor. You can explore both methods:
Using the Create Table Dialog![]()
Using the SQL Editor:
![]() Adding Table Data
Now that you have created one or more tables in the contact database, you can start populating it with data. There are several ways that you can add records to your table.
Read the sections below to learn how to use all these methods of populating the FRIENDS table with data.
Running an SQL Statement
Using the SQL Editor
Deleting Tables
In the following step, you use an external SQL script to create a new COLLEAGUES table. However, you just created a COLLEAGUES table in the Using the SQL Editor section above. In order to make it clear that the SQL script indeed creates a new table, you can delete the already created COLLEAGUES table now. To delete a database table perform the following steps.
Using an External SQL Script
Issuing commands from an external SQL script is a popular way to manage your database. You may have already created an SQL script elsewhere, and want to import it into NetBeans IDE to run it on a specified database.
In this exercise the script will create a new table named COLLEAGUES and populate it with data. Perform the following steps to run the script on the contact database.
Recreating Tables from a Different Database
If you have a table from another database which you would like to recreate in the database you are working in from NetBeans IDE, the IDE offers a handy tool for this. You first need to have the second database registered in the IDE, similar to what was described at the beginning of this tutorial. For the purposes of this tutorial, use the sample database that comes packaged with Java DB. This process is essentially carried out in two parts: You first 'grab' the table definition of the selected table, then you can recreate the table in your chosen database:
If you view the data in the new CUSTOMER table you will see that there are no records in the database, but that the structure of the table is identical to the table that you grabbed.
See Also
This concludes the Working with the Java DB (Derby) Database tutorial. This tutorial demonstrated how to set up a connection to the Java DB database in NetBeans IDE. It then demonstrated how to create, view, modify and delete tables in the IDE's Services window. It also showed how work with the SQL Editor to add data to tables, and use the IDE's functionality allowing you to recreate tables using definitions from other databases.
For related and more advanced tutorials, see the following resources:
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |