Practical Database Programming With Java
.pdf5.3 Exploring NetBeans IDE 6.8 309
A.A new entity manager instance is created since we need to use it to create and execute some queries to our Customer table in the JavaDB sample database later.
B.A Java persistence query is performed to pick up all columns from the Customer table.
C.The getResultList() method is executed to pick up returned columns stored in a Query collection, and assign them to a local variable resultList that has a List collection format.
D.An extended Java for loop is executed to get all names and cities for all customers in the Customer table. The append() method is used to attach each record to the end of the Text Area control.
Since we have not set dependencies on the modules that provide the Customer object and the persistence JARs, the statements above will be marked with red error underlines. These will be fixed in the next section.
5.3.8.5 Set Dependencies between Modules
In this section, we will enable some modules to use code from some of the other modules. We can do this very explicitly by setting intentional contracts between related modules, that is, as opposed to the accidental and chaotic reuse of code that tends to happen when we do not have a strict modular architecture such as that provided by the NetBeans Platform.
The dependencies used in this application can be described as:
1.The entity class module needs to have dependencies on the derbyclient module, as well as on the eclipselink module. To set up these dependencies, right click on the CustomerLibrary module and choose the Properties, to open the Project Properties
wizard. Select the Libraries tab and click on the Add Dependency button. On the opened wizard, select the derbyclient and eclipselink modules from the Modules list, as shown in Figure 5.158, and click on the OK button to set dependencies on the two modules that the CustomerLibrary module needs.
Your finished Project Properties wizard should match one that is shown in Figure 5.159.
Click on the OK button to finish these dependencies setup process.
2.The CustomerViewer module needs a dependency on the eclipselink module, as well as on the entity class module. To set up these dependencies, right click on the
CustomerViewer module, and choose Properties, and use the Libraries tab to set dependencies (eclipselink and CustomerLibrary) on those two modules that the CustomerViewer module needs.
3.Open the CustomerTopComponent in the Source view, right click in the editor, and
choose “Fix Imports” to open the Fix All Imports wizard and click the OK button to try to fix any missed package and dependency. The IDE is now able to add the required import statements, because the modules that provide the required classes are now available to the CustomerTopComponent.
4.A possible unsolved component may be encountered, which is the Query class. There are two Query classes located at the different packages: the javax.management and javax.persistence packages. In this application, we need the Query class that is located at the second package. To fix this problem, just prefix the package name javax.
310 Chapter 5 Introduction to NetBeans IDE
Figure 5.158. The Add Module Dependency wizard.
Figure 5.159. The finished Project Properties wizard.
persistence before the Query class, as shown in step A in Figure 5.160. The prefixing part has been highlighted in bold.
We now have set contracts between the modules in our application, giving our control over the dependencies between distinct pieces of code.
Now we are ready to build and run our module project to test its functionality.
5.3 Exploring NetBeans IDE 6.8 311
………
public CustomerTopComponent() { initComponents();
setName(NbBundle.getMessage(CustomerTopComponent.class, "CTL_CustomerTopComponent")); setToolTipText(NbBundle.getMessage(CustomerTopComponent.class, "HINT_CustomerTopComponent")); // setIcon(ImageUtilities.loadImage(ICON_PATH, true));
EntityManager entityManager = Persistence.createEntityManagerFactory("CustomerLibraryPU").createEntityManager();
Ajavax.persistence.Query query = entityManager.createQuery("SELECT c FROM Customer c"); List<Customer> resultList = query.getResultList();
for (Customer c : resultList) {
CustomerTextArea.append(c.getName() + " (" + c.getCity() + ")" + "\n");
}
}
Figure 5.160. The modified codes for the CustomerTopComponent class.
Figure 5.161. A running example of the project JavaModuleCustomer.
5.3.8.6 Build and Run the NetBeans Module Project
Click on the Clean and Build Main Project button to build our project. A successful building message should be displayed in the Output window if everything is fine.
First, let’s run our database server by right clicking on the JavaDB sample database connection URL jdbc:derby://localhost:1527/sample [app on APP]from the
Services window and select the Connect item from the popup menu. Then right click on our project JavaModuleCustomer from the Projects window, and select the Run item from the popup menu to run the project. A running result is shown in Figure 5.161.
312 Chapter 5 Introduction to NetBeans IDE
A complete NetBeans Module project JavaModuleCustomer can be found from the folder DBProjects\Chapter 5 that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1). You can download and run this application in your computer.
5.4 CHAPTER SUMMARY
The basic and fundamental knowledge and implementations of NetBeans IDE 6.8 are discussed and presented with a lot of real examples projects in this chapter. The components and architecture of NetBeans IDE 6.8 are introduced and analyzed in detail at the beginning of this chapter. Following an overview of NetBeans IDE 6.8, a detailed discussion and introduction of the NetBeans IDE 6.8 platform is given. A detailed introduction and illustration in how to download and install NetBeans IDE 6.8 are provided in this chapter.
Most popular technologies and applications supported by NetBeans IDE 6.8 are discussed, which include:
•Java Applications
•Java Desktop Applications
•Java Class Library
•JavaFX Script Applications
•JavaFX Desktop Business Applications
•JavaFX Mobile Business Applications
•Java Enterprise Edition Applications
•Java Enterprise Edition 6 Web Applications
•Apache Maven Applications
•PHP Applications
•NetBeans Module Applications
Each of these technologies and implementations is discussed and analyzed in detailed with real project examples, and line-by-line coding illustrations and explanations. Each real sample project has been compiled and built in NetBeans IDE, and can be downloaded and run at user’s computer easily and conveniently.
The main application Web and database servers, as well as databases involved in those example projects, include:
1.JavaDB Sample Database Server
2.Glassfish Enterprise Web Server v3
3.Apache HTTP Web Server
4.MySQL Database Server
5.Hibernate Query Language (HQL)
6.MySQL Database and MySQL Query Language
All of these technologies and their implementations are discussed and illustrated by using real project examples in this chapter step by step and line by line. By following
Homework 313
these example projects, users can learn and master those key techniques easily and conveniently with lower learning curves.
All actual example projects discussed and developed in this chapter have been compiled and built successfully, and stored in the folder DBProjects\Chapter 5 that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1).
HOMEWORK
I. True/False Selections
____1. The NetBeans Platform is a broad Swing-based framework on which you can base large desktop applications.
____2. Each distinct feature in a NetBeans Platform application can be provided by a distinct NetBeans module, which is comparable with a plug-in.
____3. A NetBeans module is a group of Java classes that provides an application with a specific feature.
____4. The NetBeans IDE is an open-source integrated development environment, and it only supports development of all Java application types.
____5. Three main modules included in the NetBeans IDE are: NetBeans Profiler, GUI Design Tool, and NetBeans JavaScript Editor.
____6. NetBeans IDE is mainly composed of NetBeans Open-Source IDE and NetBeans Platform.
____7. A Java Class Library is only a skeleton Java class library without a main class, but it can be executed by itself.
____8. JavaFX, which is a kind of script language, is a Java platform for creating and delivering rich Internet applications that can run across a wide variety of connected devices.
____9. The difference between a JavaFX Script Application and a JavaFX Desktop Business Application is that the former runs the application using command-line JavaFX executable with a default scene, and the latter uses a Standard Execution mode with a definite view or scene.
___10. The Java EE differs from the Java SE in that it adds libraries that provide functionality to deploy fault-tolerant, distributed, multi-tier Java software, based largely on modular components running on an application server.
II. Multiple Choices
1.Some of the fundamental components of Java EE include: ___________________.
a.Java SE and NetBeans IDE
b.NetBeans IDE platform and open source
c.Java EE Application model and specifications
d.Enterprise JavaBeans (EJB) and Java Persistence API (JPA)
2.A Java EE application is delivered in either a Java Archive (JAR) file, a(n) ___________, or a(n) ________________.
a.Application file, deployment file
b.Web file, Desktop application file
c.Web Archive (WAR) file, Enterprise Archive (EAR) file
d.Deployment file, Web Archive (WAR) file
314 Chapter 5 Introduction to NetBeans IDE
3.The most often used Java EE 6 Application server is __________________.
a.JEUS 7 application server
b.JBoss Application Server 6
c.Caucho Resin 4.0
d.GlassFish application server
4.The major Java Bean used to handle or process a message is called _____________.
a.Session Bean
b.Notification Bean
c.Message-Driven Bean
d.Manager Bean
5.The ______________ just work as a View for the Glassfish application server and set up a connection between the application server and the Session Bean in the Web tier.
a.Java EE 6
b.Enterprise Java Beans (EJB)
c.Java Server Faces (JSF)
d.Java Persistence API
6.Based on the definition of the Maven, it has two major functionalities: ____________ and
________________.
a.Project development, project implementation
b.Project building, project management
c.Project deployment, project implementation
d.Project implementation, project debugging
7.When typing a HQL query in the HQL Query Editor, for each column name you used in your query, you must use the __________ column name, not the ___________ column name in the relational data table.
a.Original, modified
b.Modified, original
c.SQL, mapped
d.Mapped, original
8.PHP is a scripting language that is particularly useful for server side development of the Web application, in which PHP will run on a Web server.
a.Script, server
b.Procedure-oriented, client
c.Web programming, server
d.Object-oriented, server
9.A PHP project can run in a _____________.
a.Local website
b.Remote website (FTP)
c.script
d.all of above
10.A module can be considered as a(n) ___________ object or unit that can be combined or bound together to form a ____________________ application.
a.Dependent, big and complex
b.Dependent, small and easier
Homework 315
c.Independent, big and complex
d.Independent, small and easier
III. Exercises
1.Provide a brief description about the Java EE 6 and its multi-tier application model.
2.Provide a brief discussion about Java EE 6 three-tier application layers.
3.Provide a brief description about JavaFX script language.
4.List the most popular application servers used by Java EE 6.
5.Refer to Section 5.3.2.2; build a similar Java Desktop Application named JavaDesktopDB and use the Manufacturer table as the target table.
6.Explain the advantages of using NetBeans Module for Java project development.
464Chapter 7 Insert, Update, and Delete Data from Databases
1.Set a connection to our target database using JPA Wizards.
2.Use JPA to build entity classes from our target database.
3.Add the entity manager and JPA components into the our applications.
4.Use entity classes to build manipulating queries to perform data manipulations.
5.Use JPA wizards to check and validate data manipulation results.
6.Disconnect our target database.
Operational steps 1–3 have been discussed and illustrated in detail with a lot of real examples in Chapter 6. Please refer to associated sections in that chapter to get more detailed information for them. In this chapter, we will start from step 4 and use some
finished projects we built in Chapter 6 as examples to illustrate how to
•Build data manipulating queries using entity classes.
•Use JPA wizards to check and validate data manipulation results.
Because the SQL Server and Oracle databases are most popular databases and widely implemented in most businesses, in this Chapter, we will concentrate on using these two kinds of databases.
To save the time and space, we can use some finished projects we built in Chapter 6 and modify some codes in certain Frames to perform data manipulations to our target databases. First, let’s start with the data insertion manipulations to the SQL Server database.
7.1 PERFORM DATA MANIPULATIONS TO SQL SERVER DATABASE USING JPA WIZARDS
Let’s take care of the data insertion to the SQL Server database using the JPA Wizards.
7.1.1 Perform Data Insertion to SQL Server Database Using JPA Wizards
We want to use and modify a finished project SelectQueryWizard we built in the last chapter to develop the data insertion function using the JPA wizards. 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 SelectQueryWizard 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 SQL Server 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:
7.1 Perform Data Manipulations to SQL Server Database Using JPA Wizards 465
•The Faculty Entity Manager has been added into the FacultyFrame class.
•The SQL Server 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.1.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 SelectQueryWizard:
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 SelectQueryWizard, 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.
3.The point to be noted is that you now have two SelectQueryWizard projects in the NetBeans IDE, but they are different projects with different functions. The first SelectQueryWizard was built in Chapter 6 without data manipulation function, but this second project will be built in Chapter 7 with the data manipulation function.
4.Expand this project files to open the FacultyFrame.java file by double clicking on this file that is located under the Source Packages\LogInFramePackage node.
5.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 fields 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.1. |
|
|
|
|
||
Table 7.1. Objects and controls added into the faculty frame window |
|
|
||||
|
|
|
|
|
|
|
Type |
Variable Name |
Text |
editable |
|
Title |
|
Label |
Label1 |
Faculty ID |
|
|
|
|
Text Field |
FacultyIDField |
|
No check |
|
|
|
Label |
Label2 |
Name |
|
|
|
|
Text Field |
FacultyNameField |
|
checked |
|
|
|
Label |
Label3 |
Faculty Image |
|
|
|
|
Text Field |
FacultyImageField |
|
checked |
|
|
|