7.1 PERFORM DATA MANIPULATIONS TO ORACLE DATABASE USING THE JAVA RUNTIME OBJECT
As we did for data query operations, in this section, we will discuss how to perform data manipula-tions using the Java runtime object method. Relatively speaking, there are some limitations in using Java API wizards to do data manipulations. For instance, after the mapped entity has been built and the entity manager object has been created, data manipulation can only be performed for that speci-fied entity object or data Table. In other words, a defined or mapped entity object cannot perform data manipulation for any other entity object or data Table.
A good solution to those limitations is to use the Java runtime object to perform the data manipu-lation, and this will provide much more flexibility and control of data manipulation against the Oracle database and allow a single object to perform multiple data manipulations against the target database.
Let’s first concentrate on data insertion to our Oracle database using the Java runtime object method.
7.2 PERFORM DATA INSERTION TO ORACLE DATABASE USING THE JAVA RUNTIME OBJECT METHOD
We have provided a very detailed and clear discussion about the Java runtime object method in Section 6.1 in Chapter 6. Refer to that section to get more details on this topic. Generally, to use the Java runtime object to perform data manipulations against our target database, the following six steps should be followed:
- Load and register the database driver using the DriverManager class and Driver methods.
- Establish an Oracle database connection using the Connection object.
- Create a data manipulation statement using the createStatement() method.
- Execute the data manipulation statement using the executeUpdate() or execute() method.
- Retrieve and check the execution result of the data manipulation.
- Close the statement and the database connection using the close() method.
FIGURE 7.1 The finished Copy Project wizard.
Generally, the Oracle database is a popular database system and has been widely implemented in most commercial and industrial applications. In this and the following sections in this chapter, we will concentrate on this database system to discuss how to perform data insert, update and delete operations.
To save time and space, we can use and modify a project we built in Chapter 6, OracleSelectFaculty, to perform data manipulations against the Faculty Table in our target database. Perform the following operations to complete this project transfer:
1) Open the Windows Explorer and create a new folder, such as Class DB Project\ Chapter 7, in your root drive.
2) Open Apache NetBeans 12.0, and find the project we built in Chapter 6, OracleSelect Faculty, in the Projects window. Otherwise, find this project in the folder Class DB Projects\Chapter 6 in the Students folder on the CRC Press ftp site.
3) Right-click on that project and select the Copy item from the popup menu to open the
Copy Project wizard.
4) Change the project name to OracleInsertFaculty in the Project Name box.
5) Browse to the folder, Class DB Projects\Chapter 7, which was created in step 1, and click on the OK button to select this location as your project location.
6) Your finished Copy Project wizard should match the one shown in Figure 7.1.
7) Click on the Copy button to complete the copy process.
Now you can find this copied project, OracleInsertFaculty, in the Project window. With this project, we are ready to build our data insertion query to perform data manipulations on our
Oracle sample database CSE _ DEPT.
In Section 6.3.1 in Chapter 6, we created a FacultyFrame class and Faculty JFrame window,FacultyFrame. Also, the following components have been added into that project:
- A JDBC driver for the Oracle 18c XE database has been loaded and registered.
- A valid database connection to that project has been established.
- A PreparedStatement instance has been created and implemented in the Select button click event handler to perform the data selection query.
In this section, we want to use the Insert button that has been built in the FacultyFrame window to perform the data insertion function. The data insertion action includes inserting a new faculty record with a new or default faculty image.