Perform Data Deleting to SQL Server Database Using

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

JAVA RUNTIME OBJECTS METHOD

7.3 PERFORM DATA MANIPULATIONS TO SQL SERVER DATABASE USING JAVA RUNTIME OBJECT

7.3.3 Perform Data Deleting to SQL Server Database Using

Basically, there is no signifi cant difference between the data updating and deleting using Java runtime object method. In this section, we try to use the Delete button we built in the FacultyFrame form window to perform this data deletion operation.

To make this deleting simple, we want to just delete the selected faculty record without touching the associated faculty image.

7.3.3.1 Develop the Codes for the Delete Button Event Handler

Open the Delete button click event handler and enter the codes that are shown in Figure 7.31 into this event handler. Let ’ s have a closer look at this piece of codes to see how it works.

A. Two local variables, numDeleted and cFacultyName , are created fi rst, and these two variables are used to hold the running result of the data deleting action and the current faculty name.

B. The deleting query string is created with one positional parameter. The query criterion is the faculty name that is placed after the WHERE clause.

C. A try … catch block is used to assist this data deleting action. First, a PreparedStatement instance is created using the Connection object that is located at the LogInFrame class with the deleting query string as the argument.

Figure 7.30. The data updated result.

c07.indd 499

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

www.traintelco.com

D. The setString() method is used to initialize the positional parameter, which is the faculty name to be deleted from the Faculty Name combo box.

E. After this faculty record has been deleted, we need to remove this faculty name from the Faculty Name combo box. In order to remember the current faculty name, we need to temporarily store it into our local string variable cFacultyName .

F. The data deleting action is performed by calling the executeUpdate() method. The delet- ing result, which is an integer number that is equal to the number of rows that have been deleted by this data deleting action, is returned and assigned to the local integer variable numDeleted .

G. The catch block is used to track and collect any possible exception encountered when this data deleting is executed.

H. The running result is printed out as a debug purpose.

I. The deleted faculty name is removed from this Faculty Name combo box.

Now we are ready to build and run the project to test the data deletion function.

7.3.3.2 Build and Run the Project to Test the Data Deleting

Make sure that our sample database CSE_DEPT has been connected to our project.

To check this connection, open the Services window and expand the Databases node to locate our sample database connection URL, jdbc:sqlserver://localhost\

SQL2008EXPRESS: 5000;databaseName= CSE_DEPT [ybai on dbo] . Right click on this URL and select the Connect item to do this connection.

Now click on the Clean and Build Main Project button from the toolbar to build our project. Then click on the Run Main Project button to run the project.

Enter suitable username and password, such as jhenry and test , to complete the login process and select the Faculty Information from the SelectFrame window to open the FacultyFrame window. Make sure that the Runtime Object Method has been selected

Figure 7.31. The developed codes for the Delete button click event handler.

private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) { int numDeleted = 0;

String cFacultyName = null;

String query = "DELETE FROM Faculty WHERE faculty_name = ?";

try {

PreparedStatement pstmt = LogInFrame.con.prepareStatement(query);

pstmt.setString(1, ComboName.getSelectedItem().toString());

cFacultyName = (String)ComboName.getSelectedItem();

numDeleted = pstmt.executeUpdate();

}

catch (SQLException e) {

msgDlg.setMessage("Error in Statement!" + e.getMessage());

msgDlg.setVisible(true);

}

System.out.println("The number of deleted row = " + numDeleted);

ComboName.removeItem(cFacultyName);

} A B C D E F G

H I

from the Query Method combo box. Then click on the Select button to query the default faculty information. The default faculty information is displayed.

To test this data deletion function, we can try to delete one faculty member, such as

Ying Bai , from our Faculty table. To do that, select this faculty member from the Faculty Name combo box, and click on the Delete button. Immediately, you can fi nd that this faculty name has been removed from the Faculty Name combo box. Also, the running result is shown in the Output window, as shown in Figure 7.32 .

To confi rm this data deletion, click on the Back and the Exit button to stop our project. Then open our Faculty table by going to the Services window and expand the

Databases node, and our connection URL, and fi nally our sample database CSE_DEPT . Expand our database schema dbo and right click on the Faculty table. Select the View

Data item from the pop - up menu to open our Faculty table. On the opened Faculty table, you can fi nd that the faculty member Ying Bai has been removed from this table.

Our data deletion function is successful!

To make our database clean and neat, it is highly recommended to recover this deleted faculty member and related records in our Faculty, LogIn, Course, and StudentCourse tables. Refer to Tables 7.2 – 7.5 in Section 7.1.3.2 to complete these data recoveries. An easy way to do this is to use the Microsoft SQL Server 2008 Management Studio. For your convenience, we will show these deleted records in Tables 7.8 – 7.11 again, and you can add or insert them back to the related tables to complete this data recovery.

As we discussed in Section 6.4.2.3 in Chapter 6 , in addition to using the executeUp- date() method to perform data manipulations, such as data insertion, updating, and delet- ing actions, one can use the execute() method to perform the similar data manipulations.

I prefer to leave this optional method as a homework and allow students to handle this issue.

A complete sample project SQLSelectObject that can be used to perform data inser- tion, updating and deletion actions against our SQL Server sample database 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 ).

Figure 7.32. The successful data deletion message.

Table 7.8. The deleted faculty record in the faculty table

faculty_id faculty_name office phone college title email

B78880 Ying Bai MTC-211 750-378-1148 Florida Atlantic University Associate Professor ybai@college.edu

c07.indd 501

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

www.traintelco.com

Next, let ’ s take care of the data manipulations against the Oracle database using the Java runtime object method.

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

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

(791 trang)