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

当前页面: 开发资料首页Netbeans 专题使用 NetBeans IDE 将 GUI 连接到 Derby 数据库(1)

使用 NetBeans IDE 将 GUI 连接到 Derby 数据库(1)

摘要: This tutorial guides you through the process of connecting the GUI of an application called ContactEditor to a Derby database. In the process you will add data aware JDBC components to the GUI that will enable the program to interact with an employee database.

In this tutorial you will learn how to:

Note that this tutorial refers to a sample project called GUI DB Exercise Initial which you should refer to while working through the steps. You can download the zip archive here. Also note that the archive includes a GUI DB Exercise Final project showing the completed application.


Getting Started


In the GUI Builder Quickstart tutorial, we concentrated on building a dialog called ContactEditorUI for the ContactEditorUI.java application. In this tutorial, we'll connect both the ContactEditorUI GUI form we created before and the main (parent) window of the application's UI to a Derby database. Note that though the tutorial images illustrate the process on Macintosh OS-X, the steps are virtually identical for other supported platforms, such as Windows or Solaris.

In order to successfully accomplish this tutorial, you must already have the IDE's included Derby database server set up and running. Note that as of NetBeans IDE 5.5 Beta, the included database is no longer called Derby and is referred to as Java DB. You also need to compile the necessary support classes that were included in the sample project (visible in the Projects window) into the project. For more information about installing and configuring Derby for use with NetBeans, see the NetBeans Derby tutorial.

Finally, you also must have already created the contact_database with both User Name and Password set to nbuser. You also must have added the necessary fields to the database using the Create Table UI or by executing the following SQL statement:


CREATE TABLE "CONTACTS" ( "ID" INTEGER not null primary key, "FIRST_NAME" VARCHAR(50), "LAST_NAME" VARCHAR(50), "TITLE" VARCHAR(50), "NICKNAME" VARCHAR(50), "DISPLAY_FORMAT" SMALLINT, "MAIL_FORMAT" SMALLINT, "EMAIL_ADDRESSES" VARCHAR(1000) )

Note that if you created the table using the SQL command instead of the Create Table UI, you need to refresh the Tables node in the Runtime window in order to see the Contacts table.


Note: In order to complete this tutorial successfully, you must be running NetBeans IDE 5.0 on JDK 5.


Adding the Data Model


Since the application's main window GUI has already been laid out for us, we can jump straight to adding the data-aware components that will enable us to interact with our data in the database. In this section, we'll add the data model that will ensure the data is presented to the UI in the correct form.
Adding a JDBCRowSet

Because the JDBCRowSet we require is already included in the GUI DB Example project, we only need to add it to our application GUI. Since the included RowSets have BeanInfos, we can use them as we would other components with the IDE displaying their properties in the Properties window. Because we're only going to use this class once, we don't need to bother installing the JDBCRowSet into the Palette in order to add it to the form. While this approach is worthwhile if you want to use a class repeatedly, we'll simply copy and paste to save time and hassle.

To add a JDBCRowSet to the form:

  1. In the Projects window, right-click the JDBCRowSet.java node and choose Copy from the pop-up menu.

  2. Paste it anywhere in the Design Area.

    The IDE adds the JDBCRowSet to the form and a node representing the row set appears in the Inspector window.

Notice that the Inspector window node is added within the Other Components node. If you receive an error when attempting to paste the row set, compile the project and then try again.

Setting the JDBCRowSet's Properties

Now we need to edit the JDBCRowSet properties so that it refers to the Contacts database we created earlier. We also need to set the driver and path it should use, as well as provide the password and username it will need to make the connection.

To edit the JDBCRowSet's properties:

  1. Select the JDBCRowSet1 node in the Inspector window (not the Projects window).

  2. In the Properties window, enter select * from contacts for the command property.

  3. Enter org.apache.derby.jdbc.ClientDriver for the driver property.

  4. Enter jdbc:derby://localhost:1527/contact_database for the url property.

  5. Enter nbuser for the password property.

  6. Enter nbuser for the username property.
Adding the Data Model

Now we need to add the data model that will be the layer between the application and our database as well as encapsulate data access, providing the logic for accessing and modifying the data itself. Once again, the necessary ContactsModel class has been provided in the example project so we'll need only add it to our application. Including such a class enables the possibility of changing the data model without necessitating changes to the GUI.

To add the data model to the form:

  1. In the Projects window, right-click the ContactsModel.java node and choose Copy from the pop-up menu.

  2. Paste it anywhere in the Design Area.

    The IDE adds the ContactModel to the form and a node representing the row set appears within the Other Components node in the Inspector window.

To change the variable name of the data model:

  1. Right-click the contactsModel1 node in the Inspector window and choose Change Variable Name from the pop-up menu.

  2. In the Rename dialog that appears, enter contactsModel for the new variable name and click OK.

To set the rowSet property of the data model:

  1. Select the contactsModel node in the Inspector window.

  2. In the Properties window, click the ellipsis button (...) for the rowSet property.

  3. In the editor that appears, select the Bean radiobutton in the Get Parameter From pane.

  4. Select jDBCRowSet1 from in the Select Bean combobox.

  5. Click OK to exit the dialog.

    The IDE sets form's contactsModel data model to use the jDBCRowSet.

top



↑返回目录
前一篇: 使用 NetBeans IDE 将 GUI 连接到 Derby 数据库(2)
后一篇: 添加按钮的功能(初学者指南)(2)