Perform Data Insertion to Oracle Database Using

Một phần của tài liệu practical database aprogramming with java (Trang 357 - 360)

JAVA PERSISTENCE API WIZARDS

7.2 PERFORM DATA MANIPULATIONS TO ORACLE DATABASE USING JPA WIZARDS

7.2.1 Perform Data Insertion to Oracle Database Using

To simplify this introduction, we can use a project OracleSelectWizard we developed in Section 6.2.9 in Chapter 6 and make some modifi cations to that project to make it as our new project. Perform the following operations to complete this project transferring:

1. Open the Windows Explorer and create a new folder, such as JavaDBProject\Chapter 7 . 2. Open a Web browser and go to the folder DBProjects\Chapter 6 that is located at the Wiley

ftp site (refer to Figure 1.2 in Chapter 1 ).

3. Copy the project OracleSelectWizard from that folder and paste it to our new folder

JavaDBProject\Chapter 7 .

Now we are ready to build our data insertion query to perform data manipulations to our Oracle sample database CSE_DEPT.

In Section 6.2.7.1 in Chapter 6 , we have created a FacultyFrame class and Faculty JFrame window FacultyFrame. Also, the following components have been added into that project:

• The Faculty Entity Manager has been added into the FacultyFrame class.

• The Oracle sample database has been connected to our project.

In this section, we want to use the Insert button that has been added into the FacultyFrame window to perform this data insertion function.

7.2.1.1 Modify the FacultyFrame Window Form

First, let ’ s modify the FacultyFrame form by adding three more Text Fields into this frame:

two of them are added into the Faculty Information panel to enable us to insert a faculty record, and one them is added at the top of the faculty image box to allow us to insert a new faculty image (exactly the location of the faculty image).

Perform the following operations to open our pasted project OracleSelectWizard : 1. Launch the NetBeans IDE 6.8 and go to File > Open Project menu item to open

the Open Project wizard.

2. Browse to the location where we copied and pasted our project OracleSelectWizard , which is JavaDBProject\Chapter 7 . Make sure that the Open as Main Project checkbox has been checked, and select this project and click on the Open Project button to open it.

The point to be noted is that you now have two OracleSelectWizard projects in the NetBeans IDE, but they are different projects with different functions. The fi rst

OracleSelectWizard was built in Chapter 6 without data manipulation function, but this second project will be built in Chapter 7 with the data manipulation function.

3. Expand this project fi les to open the FacultyFrame.java fi le by double clicking on this fi le that is located under the Source Packages\LogInFramePackage node.

4. Click on the Design button at the top of this window to open the GUI window of this FacultyFrame class.

Perform the following operations to add three more Text Fields into this frame window:

• Enlarge the FacultyFrame window form and the Faculty Information panel.

• Add two more labels and two more Text fi elds into this Faculty Information panel, and one more label and the associated Text Field to the top of the Faculty Image box with the properties shown in Table 7.6 .

One point to be noted is the FacultyIDField , and its editable property is checked, which means that we want users to insert a new faculty_id as the project runs. However, this property should not be checked when we perform a data updating action because we will not update a faculty_id during a faculty record updating process.

Your fi nished modifi ed FacultyFrame form window should match one that is shown in Figure 7.17 .

Now let ’ s develop the codes for the Insert button click event handler to perform the data insertion function as the project runs. As we did for the SQL Server database, we will use the JPQL persist() method to perform this data insertion.

7.2.1.2 Develop the Codes for the Insert Button Event Handler

As we mentioned, there is no signifi cant difference in querying a SQL Server and an Oracle database using JPA Wizard in NetBeans 6.8. The whole project SelectQueryWizard

Table 7.6. Objects and controls added into the faculty frame window

Type Variable Name Text editable Title

Label Label1 Faculty ID

Text Field FacultyIDField checked

Label Label2 Name

Text Field FacultyNameField checked

Label Label3 Faculty Image

Text Field FacultyImageField checked

c07.indd 483

c07.indd 483 7/20/2011 11:12:05 AM7/20/2011 11:12:05 AM

www.traintelco.com

we built in Chapter 6 can be used to perform data manipulations to an Oracle database with a little modifi cation. Exactly you can copy the codes in the Insert button click event handler we built in Section 7.1.1.3 and paste them into the Insert button click event handler in the FacultyFrame form in our current OracleSelectWizard project.

Four small modifi cations are important and necessary, and they are listed below:

1. The data table name used in the query strings. There is a little difference for the table names used in SQL Server database and Oracle database in the JPA mapped Entity classes. For example, the LogIn table in our sample SQL Server database CSE_DEPT is still mapped to LogIn in the LogIn.java Entity class; however, it is mapped to Login in the Login.java Entity class for the Oracle database. Make sure to check the Entity classes to confi rm and use the correct table names when different databases are utilized.

2. The query components used in the data query operations. These is little difference in the names of the JPA query components used in SQL Server database and Oracle database when they are added into each Frame Form window. For example, the LogIn JPA query object used in SQL Server database is named logInQuery ; however, it is named loginQuery when is created for the Oracle database. Make sure to check the added query components to confi rm and use the correct query components for each Frame.

3. The entity manager class name used in the Oracle database is different with that in the SQL Server database. In the project OracleSelectWizard , the name of the mapped entity manager class is OracleSelectWizardPUEntityManager ; therefore, you need to use this name to replace the SelectQueryWizardPUEntityManager , which is the name of the mapped entity manager for the SQL Server database, in all codes to perform a data manipu- lation to our Oracle sample database.

Figure 7.17. The modifi ed FacultyFrame form window.

4. The ShowFaculty() method should be modifi ed and divided into two submethods,

ShowFaculty() and DisplayImage() , to coordinate the data manipulations. Refer to Section 7.1.1.4 to complete this modifi cation.

One point to be remembered is that you can copy the codes from the Select button click event handler from the project SelectQueryWizard and paste them into the Select button click event handler in the FacultyFrame form in our current OracleSelectWizard project to perform the validation of this data insertion. The only modifi cation is to replace the SQL Server entity manager SelectQueryWizardPUEntityManager with the Oracle entity manager OracleSelectWizardPUEntityManager in that piece of codes.

Another point to be noted is that you need to copy all image fi les, including both faculty and student image fi les, from the Image folder that is located at the site ftp://

ftp.wiley.isbn/JavaDB , and paste them to your current project folder. In this case, it should be JavaDBProject\Chapter 7\OracleSelectWizard . You need also to remember to connect to our sample Oracle database we loaded in Section 6.2.1.3 when you create Entity classes for each tables in that Oracle database. Follow steps listed below to com- plete this database connection and mapping:

1. Click on the Services tab to open the Services window

2. Extend the Databases node and you can fi nd our load - in Oracle sample database

jdbc:oracle:thin:@localhost:1251:XE [CSE_DEPT on CSE_DEPT]

3. Right click on that load - in database node and select the Connect item from the pop - up menu to connect to this sample database

Now you can perform data insertion actions against our Oracle database. In order to keep our database clean and neat, it is highly recommended to delete this new inserted faculty record from our sample database after you fi nished this data insertion testing.

A complete sample project OracleSelectWizard that can be used to perform data insertion actions against Oracle Database 10g XE can be found from the folder DBProjects\

Chapter 7 that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ).

Next, let ’ s develop the codes to perform data updating actions against our Oracle database.

Một phần của tài liệu practical database aprogramming with java (Trang 357 - 360)

Tải bản đầy đủ (PDF)

(791 trang)