Practical Database Programming with Java 1st Edition Ying Bai pdf download
Practical Database Programming with Java 1st Edition Ying Bai pdf download
https://ebookname.com/product/practical-database-programming-
with-java-1st-edition-ying-bai/
https://ebookname.com/product/java-database-programming-
bible-1st-edition-john-odonahue/
https://ebookname.com/product/oracle-database-programming-using-
java-and-web-services-1st-edition-kuassi-mensah/
https://ebookname.com/product/learning-reactive-programming-with-
java-8-1st-edition-nickolay-tsvetinov/
https://ebookname.com/product/controlling-london-s-growth-donald-
l-foley/
The Politics of Knowledge in Central Asia Science
between Marx and the Market Central Asia Research Forum
1st Edition Sarah Amsler
https://ebookname.com/product/the-politics-of-knowledge-in-
central-asia-science-between-marx-and-the-market-central-asia-
research-forum-1st-edition-sarah-amsler/
https://ebookname.com/product/rosa-luxemburg-paul-frolich/
https://ebookname.com/product/negotiation-theory-and-strategy-
casebook-first-edition-russell-korobkin/
https://ebookname.com/product/central-simple-algebras-and-galois-
cohomology-2nd-edition-gille/
https://ebookname.com/product/professional-python-1st-edition-
luke-sneeringer/
Effective Teaching in Schools Theory and Practice 3rd
Edition Chris Kyriacou
https://ebookname.com/product/effective-teaching-in-schools-
theory-and-practice-3rd-edition-chris-kyriacou/
Practical Database
Programming with Java
Ying Bai
Department of Computer Science and Engineering
Johnson C. Smith University
Charlotte, North Carolina
IEEE PRESS
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under
Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of
the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance
Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470, or on the web at
www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions
Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201)
748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in
preparing this book, they make no representations or warranties with respect to the accuracy or
completeness of the contents of this book and specifically disclaim any implied warranties of merchantability
or fitness for a particular purpose. No warranty may be created or extended by sales representatives or
written sales materials. The advice and strategies contained herein may not be suitable for your situation. You
should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any
loss of profit or any other commercial damages, including but not limited to special, incidental, consequential,
or other damages.
For general information on our other products and services or for technical support, please contact our
Customer Care Department within the United States at (800) 762-2974, outside the United States at (317)
572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not
be available in electronic formats. For more information about Wiley products, visit our web site at www.
wiley.com.
10 9 8 7 6 5 4 3 2 1
Preface xxiii
Acknowledgments xxv
Chapter 1 Introduction 1
What This Book Covers 2
How This Book Is Organized and How to Use This Book 3
How to Use the Source Code and Sample Databases 5
Instructor and Customer Support 6
Homework Solutions 7
5.3.4 Build
a Java Web Application Project 214
5.3.5 Build
a Java Enterprise Edition Project 214
5.3.5.1 Overview of Java Enterprise Edition 6 215
5.3.5.2 Install and Configure Java EE 6 Software and Tools 222
5.3.5.3 Create a Java EE 6 Web Application Project 224
5.3.5.4 Creating the Entity Classes from the Database 227
5.3.5.5 Creating Enterprise Java Beans 229
5.3.5.6 Using JavaServer Faces (JSF) 2.0 232
5.3.5.7 Creating the Manufacturer Managed Bean 234
5.3.5.8 Creating the Manufacturer Listing Web Page 235
5.3.5.9 Building and Running the First Java EE 6 Web Page 238
5.3.5.10Deploying the Project Using the Administration Console 239
5.3.5.11Creating the Manufacturer Details Web Page 241
5.3.5.12Creating and Editing the faces-config.xml Configuration File 242
5.3.5.13Editing the General Web Application Configuration
File web.xml 247
5.3.5.14 Modifying the JSF Pages to Perform Page Switching 248
5.3.5.15 Building and Running the Entire Java EE 6 Project 249
5.3.6 Build a Maven Project 251
5.3.6.1 Introduction to Maven 251
5.3.6.2 Introduction to Hibernate Framework 253
5.3.6.3 Installing and Configuring the Apache Maven 255
5.3.6.4 Configuring Maven Inside the NetBeans IDE 258
5.3.6.5 Creating a Maven Database Application Project 259
5.3.6.6 Adding Hibernate Files and Dependencies 261
5.3.6.7 Generating Hibernate Mapping Files and Java Classes 265
5.3.6.8 Creating the Application GUI 268
5.3.6.9 Creating the Query in the HQL Query Editor 270
5.3.6.10 Adding the Query to the GUI Form 272
5.3.7 Build a PHP Project 276
5.3.7.1 Introduction to PHP 276
5.3.7.2 Downloading and Installing Apache HTTP Web Server 277
5.3.7.3 Configuring and Testing the Installed Apache HTTP
Web Server 279
5.3.7.4 Downloading and Installing the PHP Engine 280
5.3.7.5 Testing the Installed PHP Engine 281
5.3.7.6 Creating a PHP Project 283
5.3.7.7 Downloading and Configuring MySQL Database Server 285
5.3.7.8 Configuring the MySQL Server in NetBeans IDE 288
5.3.7.9 Creating Our Sample Database MySQLSample 290
5.3.7.10 Building the Functions for the PHP Project 293
5.3.7.11 Running and Testing the PHP Project 297
5.3.8 Build a NetBeans Module 299
5.3.8.1 Create a New NetBeans Module Project 300
5.3.8.2 Create the Customer Entity Class and Wrap It into a Module 301
5.3.8.3 Create Other Related Modules 303
5.3.8.4 Create the User Interface Module 306
6.2.8 Use Java JPA Wizards to Query the Course Table 372
6.2.8.1 Create a New CourseFrame Class and Add It into Our Project 372
6.2.8.2 Add Course Entity Manager and JPA Components into
the Project 373
6.2.8.3 Use Entity Classes to Perform Data Query from the
Course Table 374
6.2.9 Use Java JPA Wizards to Query Oracle Database 381
6.5 Create a Java Application Project to Access the Oracle Database 441
6.5.1 Create Graphic User Interfaces 442
6.5.2 Perform the Data Query for the LogIn Table 442
6.5.2.1 Add Oracle JDBC Driver to the Project 442
6.5.2.2 Load and Register Oracle JDBC Driver 443
6.5.2.3 The JDBC Uniform Resource Locators (URLs) 443
6.5.3 Develop the Codes for the SelectionFrame Form 445
6.5.4 Perform the Data Query for the Faculty Table 445
6.5.4.1 Create an Oracle Package FacultyInfo 446
6.5.4.2 Develop the Codes to Perform the CallableStatement Query 447
6.5.5 Perform the Data Query for the Course Table 449
6.5.5.1 Create an Oracle Package FacultyCourse 449
6.5.5.2 Develop the Codes to Perform the CallableStatement Query 451
6.5.6 Query Data from the Student Table Using the Java RowSet Object 453
6.5.6.1 Modify the Codes in the Constructor of the
StudentFrame Class 453
6.5.6.2 Modify the Codes in the Select Button Click Event Handler 453
6.6 Chapter Summary 455
Homework 457
Section II Insert, Update and Delete Data Using Java Runtime Objects Method 488
8.4.3 Create Five Web Pages Using Microsoft Office Publisher 2007 614
8.4.3.1 Create the LogIn Page 615
8.4.3.2 Create the Selection Page 617
8.4.3.3 Create the Faculty Page 619
8.4.3.4 Create the Course Page 622
8.4.3.5 Create the Student Page 625
8.5 Build Java Web Project to Access SQL Server Database 625
8.5.1 Access and Query the LogIn Table Using JSP and Help Class Files 626
8.5.1.1 Modify the LogIn.jsp Page and Create LogInQuery.jsp File 627
8.5.1.2 Create the Java Help Class File LogInQuery.java 629
8.5.1.3 Create a Dialog Box as the Message Box 630
8.5.1.4 Develop the Codes for the Help Class File 632
8.5.1.5 Add the JDBC Driver for the SQL Server Database into
the Project 635
8.5.2 Build the Selection Page 637
8.5.3 Query the Faculty Table Using JSP and JSP Implicit
Session Object 640
8.5.3.1 Modify the Faculty.jsp Page 641
8.5.3.2 Create the FacultyProcess.jsp Page 642
8.5.3.3 Create the Help Class File FacultyQuery.java 645
8.5.4 Insert New Records to the Faculty Table Using JSP and Java Beans 650
8.5.4.1 Modify the Java Help Class FacultyQuery to Make it Java
Bean Class 651
8.5.4.2 Modify the FacultyProcess.jsp Page to Handle Faculty Data
Collection and Insertion 652
8.5.5 Update and Delete Data from the Faculty Table Using JSP and Java
Beans Techniques 656
8.5.5.1 Create a New Java Session Bean Class 656
8.5.5.2 Modify the FacultyProcess Page to Handle Faculty
Data Updating 659
8.5.5.3 Add a Method to the Session Bean to Perform Faculty
Data Deleting 663
8.5.5.4 Modify the FacultyProcess Page to Handle Faculty
Data Deleting 664
8.5.6 Query Data from the Course Table Using JavaServer Faces and
Java Beans 668
8.5.6.1 Modify the Course Page to Make it JavaServer Face Page 668
8.5.6.2 Build the JavaServer Face Managed Bean CourseBean 672
8.5.6.3 Build the Session Bean for Entity Classes CourseFacade 676
8.5.6.4 Set Up Calling Relationship between the JSF Bean and
the Session Bean 680
8.5.6.5 Build and Run the Project to Test the Course Information
Query Functions 681
8.5.7 Update Records from the Course Table Using JavaServer Faces and Java
Beans 682
8.5.7.1 Create Codes for the Update() Method in the JSF
Managed Bean 683
9.5 Build Java Web Service Projects to Access SQL Server Database 787
9.5.1 Create a New Java Web Application Project WebServiceSQLApp 787
9.5.2 Create a New Java SOAP-Based Web Service Project WebServiceSQL 788
9.5.3 Add Desired Operations to the Web Service 789
9.5.4 Add New Operations to Our Web Services to Perform Data Query 790
9.5.5 Build the User-Defined Method DBConnection() 792
9.5.6 Deploy the Web Service Project and Test the Data Query Function 793
9.6 Build a Windows-Based Web Client Project to Consume the Web Service 795
9.6.1 Copy the FacultyFrame and MsgDislog Components as GUIs 795
9.6.2 Create a Web Service Reference for Our Windows-Based Client
Project 797
9.6.3 Develop the Codes to Call Our Web Service Project 799
9.6.4 Build and Run Our Client Project to Query Faculty Data via
Web Service 801
9.7 Build a Web-Based Client Project to Consume the Web Service 801
9.7.1 Create a Web-Based Client Project WebClientSQL 802
9.7.2 Create a Java Managed Bean FacultyMBean and Add the JDialog
Class MsgDialog 803
9.7.3 Create a Web Service Reference for Our Web-Based Client Project 804
9.7.4 Build the Codes to Call the Web Service to Perform Data Query 805
9.7.5 Build and Run Our Client Project to Query Faculty Data via
Web Service 808
9.8 Build Java Web Service to Insert Data into the SQL Server Database 808
9.8.1 Add a New Operation InsertFaculty() into Our Web Service Project 809
9.8.2 Deploy the Web Service Project 811
9.9 Build a Windows-Based Web Client Project to Consume the Web Service 811
9.9.1 Refresh the Web Service Reference for Our Windows-Based
Client Project 812
9.9.2 Develop the Codes to Call Our Web Service Project 812
9.9.3 Build and Run Our Client Project to Insert Faculty Data via
Web Service 814
9.10 Build a Web-Based Client Project to Consume the Web Service 815
9.10.1 Refresh the Web Service Reference for Our Web-Based
Client Project 816
9.10.2 Develop the Codes to Call Our Web Service Project 816
9.10.3 Build and Run Our Client Project to Insert Faculty Data via
Web Service 818
9.11 Build Java Web Service to Update and Delete Data from the SQL
Server Database 819
9.11.1 Add a New Operation UpdateFaculty() to Perform the Faculty
Data Updating 820
9.11.2 Add a New Operation DeleteFaculty() to Perform the Faculty
Data Deleting 822
9.11.3 Deploy and Test the Web Service Project 824
9.12 Build a Windows-Based Web Client Project to Consume the Web Service 827
9.12.1 Refresh the Web Service Reference for Our Windows-Based
Client Project 827
9.12.2 Develop the Codes to Call Our Web Service Project 827
9.12.2.1 Build the Codes to Call the UpdateFaculty() Operation 827
9.12.2.2 Build the Codes to Call the DeleteFaculty() Operation 830
9.12.3 Build and Run Our Client Project to Update and Delete Faculty Record via
Web Service 831
9.13 Build a Web-Based Client Project to Consume the Web Service 834
9.13.1 Refresh the Web Service Reference for Our Web-Based Client Project 834
9.13.2 Develop the Codes to Call Our Web Service Operation
UpdateFaculty() 835
9.13.3 Develop the Codes to Call Our Web Service Operation
DeleteFaculty() 837
9.13.4 Build and Run Our Client Project to Update and Delete Faculty Record via
Web Service 838
9.14 Build Java Web Service Projects to Access Oracle Databases 840
9.14.1 Create a New Java Web Application Project WebServiceOracleApp 841
9.14.2 Create a New Java SOAP-Based Web Service Project
WebServiceOracle 842
9.14.3 Add a JDialog Class into the Web Services Project 843
9.14.4 Add Java Persistence API and Entity Classes from Database 843
9.14.5 Add Java Session Beans for Entity Classes 845
9.14.6 The Organization of Web Service Operations and Session Bean
Methods 848
9.14.7 Add the Session Bean Classes CourseFacade into Our Web Service 849
9.14.8 Create and Build the Session Bean Methods and Web Service
Operations 849
9.14.8.1 Create and Build Session Bean Method getCourseID() 850
9.14.8.2 Create and Build Web Service Operation QueryCourseID() 852
9.14.8.3 Build and Run the Web Service to Test the course_id Query
Function 854
9.14.8.4 Create and Build Session Bean Method getCourse() 855
9.14.8.5 Create and Build Web Service Operation QueryCourse() 857
9.14.8.6 Build and Run the Web Service to Test the Course Query
Function 858
9.14.8.7 Create and Build Session Bean Method newCourse() 860
9.14.8.8 Create and Build Web Service Operation InsertCourse() 862
9.14.8.9 Build and Deploy the Web Service Project 863
9.14.8.10 Create and Build Session Bean Method setCourse() 864
9.14.8.11 Create and Build Web Service Operation UpdateCourse() 867
9.14.8.12 Build and Deploy the Web Service Project 868
9.14.8.13 Create and Build Session Bean Method removeCourse() 868
9.14.8.14 Create and Build Web Service Operation DeleteCourse() 870
9.14.8.15 Build and Test the Web Service Project 871
9.15 Build a Windows-Based Web Client Project to Consume the Web Service 873
9.15.1 Create a New Windows-Based Web Client Project WinClientOracle 873
9.15.2 Copy the CourseFrame and MsgDislog Components as GUIs 874
9.15.3 Create a Web Service Reference for Our Windows-Based Client
Project 875
9.15.4 Develop the Codes to Call Our Web Service Project 876
9.15.4.1 Build Codes for the Select Button Method to Query CourseIDs 877
9.15.4.2 Build Codes for the CourseListValueChanged() Method to Get Course
Details 879
9.15.4.3 Build Codes for the Insert Button Method to Insert Courses 882
9.15.4.4 Build Codes for the Update Button Method to Update Courses 885
9.15.4.5 Build Codes for the Delete Button Method to Delete Courses 888
9.16 Build a Web-Based Web Client Project to Consume the Web Service 890
9.16.1 Create a Web-Based Client Project WebClientOracle 890
9.16.2 Create a Java Managed Bean CourseBean and Add the JDialog Class
MsgDialog 891
9.16.3 Create a Web Service Reference for Our Web-Based Client Project 893
9.16.4 Develop the Codes to Call Our Web Service Project 894
9.16.4.1 Build Codes for the Select Button Method to Query CourseIDs 894
9.16.4.2 Build Codes for the Detail Button Method to Get Course
Details 897
9.16.4.3 Build Codes for the Update Button Method to Update Courses 899
9.16.4.4 Build Codes for the Delete Button Method to Delete Courses 901
9.17 Chapter Summary 904
Homework 905
Index 909
D atabases have become an integral part of our modern day life. We are an information-
driven society. Database technology has a direct impact on our daily lives. Decisions are
routinely made by organizations based on the information collected and stored in data-
bases. A record company may decide to market certain albums in selected regions based
on the music preference of teenagers. Grocery stores display more popular items at the
eye level and reorders are based on the inventories taken at regular intervals. Other
examples include patients’ records in hospitals, customers’ account information in banks,
book orders by the libraries, club memberships, auto part orders, winter cloth stock by
department stores, and many others.
In addition to database management systems, in order to effectively apply and imple-
ment databases in real industrial or commercial systems, a good graphic user interface
(GUI) is needed to enable users to access and manipulate their records or data in data-
bases. NetBeans IDE is an ideal candidate to be selected to provide this GUI functional-
ity. Unlike other programming languages, Java is a kind of language that has advantages,
such as easy to earn and easy to be understood, with little learning curves. Beginning from
Java 1.0, Sun has integrated a few programming languages, such as C++, JavaFX, and PHP,
with some frameworks into dynamic models that make Internet and Web programming
easy and simple, and any language integrated in this model can be used to develop pro-
fessional and efficient Web applications that can be used to communicate with others via
Internet.
This book is mainly designed for college students and software programmers who
want to develop practical and commercial database programming with Java and relational
databases, such as Microsoft Access, SQL Server 2008, and Oracle Database 10 g XE. The
book provides a detailed description about the practical considerations and applications
in database programming with Java and authentic examples and detailed explanations.
More important, a new writing style is developed and implemented in this book, com-
bined with real examples, to provide readers with a clear picture as how to handle the
database programming issues in NetBeans IDE environment.
The outstanding features of this book include but are not limited to the following.
1. A novel writing style is adopted to attract students or beginning programmers who are
interested in learning and developing practical database programs, with the hope of avoiding
the headaches caused by huge blocks of codes found in traditional database programming
books.
2. A real completed sample database, CSE_DEPT, with three versions (Microsoft Access 2007,
SQL Server 2008, and Oracle Database 10 g XE Release 2), is provided and used for the
entire book. A step-by-step, detailed description about how to design and build a practical
relational database is provided.
xxiii
3. Both fundamental and advanced database programming techniques are covered, for the
convenience of both beginning students and experienced programmers.
4. Updated Java database programming techniques, such as Java Persistence API, Java
Enterprise Edition 6, JavaServer Pages, JavaServer Faces, and Enterprise Java Beans, are
discussed and analyzed with real projects to enable readers to have a clear picture and
easy-to-learn path for Java database applications.
5. More than 30 real sample database programming projects are covered, with detailed illustra-
tions and explanations to help students to understand key techniques and programming
technologies.
6. Three types of popular databases are covered and discussed in detail with practical sample
examples: Microsoft Access, SQL Server 2008, and Oracle Database 10 g Express Edition
(XE).
7. The various actual JDBC APIs and JDBC drivers are discussed and presented with real
example coding explanations. The working structure and principle of using a JDBC driver
to establish a valid database connection, build an SQL statement, and process the query
results are introduced in detail with example codes. JDBC RowSet, a useful tool, is also
discussed and analyzed with some example codes.
8. Problems and selected solutions are provided for each chapter to strengthen and improve
understanding of the topics.
9. Power Point teaching slides are also provided to help instructors.
I sincerely hope that this book will be useful to all who adopt it, as a textbook for
college students, as well as a reference book for programmers, software engineers,
and academic researchers. I would be more than happy to know that you have been able
to develop and build professional and practical database applications with the help of
this book.
YING BAI
First, I thank my wife, Yan Wang, in particular. I could not have finished this book
without her sincere encouragement and support.
I also thank Satish Bhalla, who made important contributions to Chapter 2. Dr. Bhalla
is a specialist in database programming and management, in particular, in SQL Server,
Oracle, and DB2. Dr. Bhalla spent much time preparing materials for the first part of
Chapter 2, and this is gratefully acknowledged.
Many thanks also go to Mary Mann at Wiley, who helped to make this book available
to the public, and for her deep perspective and hard work. The same thanks are extended
to the editorial team, without whose contributions the book would not have been
published.
Thanks should also be extended to the following book reviewers for their important
feedback on the manuscript:
• Dr. Jifeng Xu, Research Scientist, Boeing Company
• Dr. Xiaohong Yuan, Associate Professor, Department of Computer Science, North Carolina
A&T State University
• Dr. Daoxi Xiu, Application Analyst Programmer, North Carolina Administrative Office of
the Courts
• Dr. Dali Wang, Assistant Professor, Department of Physics and Computer Science, Christopher
Newport University
Finally, thanks should be given to all of the people who supported me in the comple-
tion of this book.
xxv
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com