Perform Data Manipulations to Oracle Database Using

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

JAVA RUNTIME OBJECTS METHOD

7.6 PERFORM DATA MANIPULATIONS USING CALLABLE STATEMENTS

7.6.2 Perform Data Manipulations to Oracle Database Using

Basically, there is no signifi cant difference between the data manipulations to SQL Server and Oracle databases using the CallableStatement object. The only differences are the connected database and stored procedures. As long as a valid connection to the selected database has been set up and all stored procedures are built using the Oracle Database 10g XE, all codes developed in Section 7.6.1 can be used for Oracle database without problem.

To save time and space, we can use and modify an Oracle project OracleSelectObject we built in Section 7.4 to make it as our new project to perform the data manipulations using the CallableStatement method.

In this section, we want to use the CourseFrame form to perform data manipulations, such as data insertion, updating, and deleting against the Course table in our Oracle sample database. The only modifi cations to this form are:

1. The CourseFrame form window 2. Three Oracle stored procedures

The fi rst modifi cation enables us to have all text fi elds and buttons in the CourseFrame form to allow us to perform all three kinds of data manipulations. The second modifi ca- tion enables us to call these Oracle stored procedures using the CallableStatement to perform the data manipulations. Let ’ s start with the fi rst modifi cation.

7.6.2.1 Modify the CourseFrame Form Window

Refer to Section 7.6.1.1.1 to complete this CourseFrame form modifi cation. Your modifi ed CourseFrame form window should match one that is shown in Figure 7.67 .

Now, you can open the project SQLSelectObject we built in the last section and copy the codes from the Insert , Update , and Delete button click event handlers in the CourseFrame form and paste them into the associated Insert , Update , and Delete button click event handlers in our current modifi ed CourseFrame form one by one. To open the project SQLSelectObject , you can go to the folder DBProjects\Chapter 7 that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ).

Now, let ’ s do some modifi cations to these three event handlers to make them match to the data actions in Oracle database.

Figure 7.67. The modifi ed CourseFrame form window.

c07.indd 541

c07.indd 541 7/20/2011 11:12:10 AM7/20/2011 11:12:10 AM

www.traintelco.com

The only modifi cation we need to do is to remove the prefi x dbo for each CallableStatement query string in three event handlers. As you remember, when we built our three stored procedures in Microsoft SQL Server Management Studio Express, dbo.

InsertNewCourse , dbo.UpdateCourse and dbo.DeleteCourse , all of these stored pro- cedures are prefi xed with a prefi x dbo , which is a SQL Server database schema. However, when we build those stored procedures in Oracle database, we do not need those prefi xes anymore. Therefore, we need to remove these prefi xes before each stored procedure in the CallableStatement query string.

Open these three event handlers and remove the prefi x dbo from each query string in our pasted codes. After this deletion, our three stored procedures are named

InsertNewCourse() , UpdateCourse() , and DeleteCourse() , respectively. Next, we will build three stored procedures with the identical names of those three stored procedures using the Object Browser in Oracle Database 10g Express Edition.

7.6.2.2 Build Three Oracle Stored Procedures

Recall in Section 6.5.4.1 in Chapter 6 , we provided a very detailed discussion about the package and stored procedures in the Oracle database environment. Refer to that section to get more detailed information about how to create a package and stored procedure in Oracle database. In this section, we will provide some discussions about how to create Oracle stored procedures, InsertNewCourse() , UpdateCourse() , and DeleteCourse() .

From Section 6.5.4.1 , we got an idea about the difference between an Oracle stored procedure and an Oracle package. The key issue is that an Oracle stored procedure never return any data, but an Oracle package must return some data. For our course data inser- tion, updating, and deleting, we do not need to return any data. Based on this criterion, let ’ s start to build our Oracle stored procedures to perform these three kinds of data actions against our sample Oracle database.

7.6.2.2.1 Create the InsertNewCourse Stored Procedure Open the Oracle Database 10g XE home page by going to Start|All Programs|Oracle Database 10g Express Edition|Go To Database Home Page items. Log in as a user by entering

CSE_DEPT into the Username box and the password reback into the Password box. Click the Object Browser and select Create|Procedure to open the Create Procedure window.

Enter the procedure name, InsertNewCourse , into the Procedure Name fi eld, and keep the Include Arguments checkbox checked, then click on the Next button.

In the Arguments page, enter seven pieces of new course information into seven Argument fi elds. Refer to Section 2.11.2.3 in Chapter 2 for data types of these seven data.

Your fi nished Arguments page should match one that is shown in Figure 7.68 . Click on the Next button to go to the procedure - defi ning page.

Enter the codes that are shown in Figure 7.69 into this new procedure as the body of the procedure using the language that is called Procedural Language Extension for SQL or PL - SQL. Then click on the Next and the Finish buttons to confi rm creating of this procedure.

Your fi nished Procedure - Defi ne page is shown in Figure 7.70 .

Seven input parameters are listed at the beginning of this procedure with the keyword IN to indicate that these parameters are inputs to the procedure. The intermediate param- eter facultyID is obtained from the fi rst query in this procedure from the Faculty table.

Figure 7.68. The fi nished Arguments page.

Figure 7.69. The body of the stored procedure.

SELECT faculty_id INTO facultyID FROM Faculty WHERE faculty_name = FacultyName;

INSERT INTO Course VALUES (CourseID, Course, Credit,

Classroom, Schedule, Enroll, facultyID);

The data type of each parameter is indicated after the keyword IN , and it must be identi- cal with the data type of the associated data column in the Course table. An IS command is attached after the procedure header to indicate that a query result, faculty_id , will be held by a local variable facultyID declared later.

Two queries are included in this procedure. The fi rst query is used to get the faculty_

id from the Faculty table based on the input parameter FacultyName, and the second query is to insert seven input parameters into the Course table based on the faculty_id obtained from the fi rst query. A semicolon must be attached after each PL - SQL statement and after the command end .

One important issue is that you need to create one local variable facultyID and attach it after the IS command as shown in Figure 7.71 , and this coding has been high- lighted with the background color. Click the Edit button to add this local variable after the IS command. This local variable is used to hold the returned faculty_id from the execution of the fi rst query.

Another important issue in distributing the input parameters or arguments in an INSERT command is that the order of those parameters or arguments must be identical

c07.indd 543

c07.indd 543 7/20/2011 11:12:10 AM7/20/2011 11:12:10 AM

www.traintelco.com

with the order of the data columns in the associated data table. For example, in the

Course table, the order of the data columns is: course_id , course , credit , classroom ,

schedule , enrollment , and faculty_id . Accordingly, the order of input parameters placed in the INSERT argument list must be identical with the data columns ’ order dis- played above.

Figure 7.71. The modifi ed stored procedure.

facultyID VARCHAR2(10);

Figure 7.70. The fi nished procedure - defi ne page.

To make sure that this procedure is error free, we need to compile it fi rst. Click the

Compile button to compile and check our procedure. A successful compilation message should be displayed if our procedure is a bug - free stored procedure.

Next, let ’ s continue to build our second stored procedure UpdateCourse() .

7.6.2.2.2 Create the UpdateCourse Stored Procedure Click on the Create button located at the upper - right corner of this InsertNewCourse() procedure window and select the Procedure to open a new procedure page.

Enter UpdateCourse into the Procedure Name fi eld and keep the Include Arguments checkbox checked, then click on the Next button.

In the Arguments page, enter six pieces of updated course information into six Argument fi elds. Refer to Section 2.11.2.3 in Chapter 2 for data types of these seven data.

Your fi nished Arguments page should match one that is shown in Figure 7.72 .

A point to be noted is that some input parameters, such as Course, Schedule, Classroom, Credit, and Enroll, are closely identical with the names of associated columns in our Course table. The only difference between them is that the fi rst letters of those input parameters are capital, but the columns ’ names are not. However, as you know, the PL - SQL language is a case - insensitive language, and this difference would be removed when the stored procedure is executed. To avoid this confusion between the input param- eters and names of columns in the Course table, we prefi xed an in before those input parameters to distinguish them with the names of associated columns in our Course table.

Click on the Next button to go to the procedure - defi ning page.

Enter the codes that are shown in Figure 7.73 into this new procedure as the body of the procedure using the PL - SQL language. Then click on the Next and the Finish buttons to confi rm creating of this procedure.

Figure 7.72. The fi nished Arguments page.

c07.indd 545

c07.indd 545 7/20/2011 11:12:10 AM7/20/2011 11:12:10 AM

www.traintelco.com

Your fi nished Procedure - Defi ne page is shown in Figure 7.74 .

To make sure that this procedure is error free, we need to compile it fi rst. Click the

Compile button to compile and check our procedure. A successful compilation message should be displayed if our procedure is a bug - free stored procedure.

Next, let ’ s continue to build our last stored procedure DeleteCourse() .

7.6.2.2.3 Create the DeleteCourse Stored Procedure Click on the Create button located at the upper - right corner of this UpdateCourse() procedure window and select the Procedure to open a new procedure page.

Enter DeleteCourse into the Procedure Name fi eld and keep the Include Arguments checkbox checked, then click on the Next button.

In the Arguments page, enter a CourseID for which the associated course will be deleted into the fi rst Argument fi eld. Refer to Section 2.11.2.3 in Chapter 2 to get this data type. Your fi nished Arguments page should match one that is shown in Figure 7.75 .

Click on the Next button to go to the procedure - defi ning page.

Enter the codes that are shown in Figure 7.76 into this new procedure as the body of this procedure using the PL - SQL language. Then click on the Next and the Finish buttons to confi rm creating of this procedure.

Your fi nished Procedure - Defi ne page is shown in Figure 7.77 .

Figure 7.74. The fi nished UpdateCourse stored procedure.

Figure 7.73. The body of the stored procedure.

UPDATE Course

SET course = inCourse, credit = inCredit, classroom = inClassroom, Schedule = inSchedule, enrollment = inEnroll WHERE course_id = CourseID;

To make sure that this procedure is error free, we need to compile it fi rst. Click the

Compile button to compile and check our procedure. A successful compilation message should be displayed if our procedure is a bug - free stored procedure.

At this point, we have fi nished building all three stored procedures in the Oracle database environment. Now close the Oracle Database 10g XE, and we can test the codes we made in Section 7.6.2.1 to call them to perform the associated data actions.

7.6.2.3 Build and Run the Project to Test the Data Manipulations

Launch NetBeans IDE 6.8 and our project OracleSelectObject . Click on the Clean and Build Main Project button to build the project. Then click on the Run Main Project button to run the project.

Figure 7.75. The fi nished Arguments page.

Figure 7.76. The body of the stored procedure.

DELETE FROM Course WHERE course_id = CourseID;

Figure 7.77. The fi nished stored procedure DeleteCourse().

c07.indd 547

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

www.traintelco.com

Enter suitable username and password, such as jhenry and test , to the LogIn frame form and select the Course Information from the SelectFrame window to open the CourseFrame form window. Make sure that the Java Callable Method has been selected from the Query Method combo box. Then click on the Select button to query the default course information for the selected faculty member Ying Bai .

Now enter the following data into seven text fi elds as a new course record for the selected faculty member:

Course ID: CSE - 549 Course: Fuzzy Systems Schedule: T - H: 1:30 – 2:45 pm Classroom: TC - 302

Credit: 3 Enrollment: 25

Then click on the Insert button to insert this course record into the Course table in our sample database.

To confi rm and validate this data insertion, click on the Select button to try to retrieve all courses taught by the selected faculty member Ying Bai . The running result is shown in Figure 7.78 , and you can see that the new inserted course CSE - 549 is indeed added to the database and displayed in the CourseList listbox.

Now select the course CSE - 549 from the CourseList listbox and enter the following data into six text fi elds as an updated course record for the selected course CSE - 549:

Course: Intelligent Controls Schedule: M - W - F: 11:00 – 11:50 a.m.

Figure 7.78. The running result for calling the InsertNewCourse() stored procedure.

Classroom: TC - 303 Credit: 3 Enrollment: 28

Then click on the Update button to update this course record in the Course table in our sample database. To confi rm and validate this data updating, click on the Select button to try to retrieve all courses taught by the selected faculty member Ying Bai . The running result is shown in Figure 7.79 .

Another way to confi rm this data updating action is to open the Course table using the Services window in the NetBeans IDE. To do that, open the Services window and expand the Databases node and connect to our Oracle database by right clicking on that URL and select the Connect item. Then expand that connected URL and our CSE_DEPT database node and Tables nodes. Right click on the Course table and select the View Data to open this table. Click on the Next Page tab and you can fi nd that the course CSE - 549 has been updated and displayed at the last line on this Course table, as shown in Figure 7.80 .

Now select the course CSE - 549 from the CourseList listbox and click on the Delete button to try to delete this course from the Course table in our sample database.

To confi rm and validate this data deletion action, click on the Select button again to try to retrieve all courses taught by the default faculty Ying Bai . It can be found that there is no CSE - 549 course in the CourseList listbox, and this means that the course CSE - 549 has been deleted from the Course table. You can also confi rm this data deleting action by opening the Course table using the Services window in the NetBeans IDE.

At this point, we have fi nished developing and building data manipulations project to Oracle database using CallableStatement object method. A complete project

OracleSelectObject that contains all three data manipulation actions to the Oracle

Figure 7.79. The running result for calling the UpdateCourse() stored procedure.

c07.indd 549

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

www.traintelco.com

database can be found at the folder DBProjects\Chapter 7 , which is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ).

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

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

(791 trang)