Java EE 8 Development with Eclipse
eBook - ePub

Java EE 8 Development with Eclipse

Develop, test, and troubleshoot Java Enterprise applications rapidly with Eclipse, 3rd Edition

Ram Kulkarni

Partager le livre
  1. 596 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Java EE 8 Development with Eclipse

Develop, test, and troubleshoot Java Enterprise applications rapidly with Eclipse, 3rd Edition

Ram Kulkarni

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

Develop and deploy fully functional applications and microservices utilising Tomcat, Glassfish servers, Cloud and docker in Java EE 8About This Book‱ Explore the complete workflow of developing enterprise Java applications‱ Develop microservices with Docker Container and deploy it in cloud‱ Simplify Java EE application developmentWho This Book Is ForIf you are a Java developer with little or no experience in Java EE application development, or if you have experience in Java EE technology but are looking for tips to simplify and accelerate your development process, then this book is for you.What You Will Learn‱ Set up Eclipse, Tomcat, and Glassfish servers for Java EE application development‱ Use JSP, Servlet, JSF, and EJBs to create a user interface and write business logic‱ Create Java EE database applications using JDBC and JPA‱ Handle asynchronous messages using MDBs for better scalability‱ Deploy and debug Java EE applications and create SOAP and REST web services‱ Write unit tests and calculate code coverage‱ Use Eclipse MAT (Memory Analysis Tool) to debug memory issues‱ Create and deploy microservicesIn DetailJava EE is one of the most popular tools for enterprise application design and development. With recent changes to Java EE 8 specifications, Java EE application development has become a lot simpler with the new specifications, some of which compete with the existing specifications. This guide provides a complete overview of developing highly performant, robust and secure enterprise applications with Java EE with Eclipse.The book begins by exploring different Java EE technologies and how to use them (JSP, JSF, JPA, JDBC, EJB, and more), along with suitable technologies for different scenarios. You will learn how to set up the development environment for Java EE applications and understand Java EE specifications in detail, with an emphasis on examples. The book takes you through deployment of an application in Tomcat, GlassFish Servers, and also in the cloud. It goes beyond the basics and covers topics like debugging, testing, deployment, and securing your Java EE applications. You'll also get to know techniques to develop cloud-ready microservices in Java EE.Style and approachThis guide takes a step-by-step approach to developing, testing, debugging, and troubleshooting Java EE applications, complete with examples and tips.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Java EE 8 Development with Eclipse est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Java EE 8 Development with Eclipse par Ram Kulkarni en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming in Java. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788833882
Édition
3

Creating JEE Database Applications

In the previous chapter, we learned how to use source control management software from Eclipse. Specifically, we learned how to use SVN and Git from Eclipse. In this chapter, we will get back to discussing JEE application development. Most web applications today require access to the database. In this chapter, we will learn two ways to access databases from JEE web applications: using JDBC APIs, and using JPA APIs.
JDBC4 has been part of JDK since version 1.1. It provides uniform APIs to access different relational databases. Between JDBC APIs and the database sits the JDBC driver for that database (either provided by the vendor of the database or a third-party vendor). JDBC translates common API calls to database-specific calls. The results returned from the database are also converted into objects of common data access classes. Although JDBC APIs require you to write a lot more code to access the database, it is still popular in JEE web applications because of its simplicity, flexibility of using database-specific SQL statements, and low learning curve.
JPA is the result of Java Specification Request 220 (which stands for JSR). One of the problems of using JDBC APIs directly is converting object representation of data to relation data. Object representation is in your JEE application, which needs to be mapped to tables and columns in the relational database. The process is reversed when handling data returned from the relational database. If there is a way to automatically map object-oriented representation of data in web applications to relational data, it would save a lot of developer time. This is also called object-relational mapping (ORM). Hibernate (http://hibernate.org/) is a very popular framework for ORM in Java applications.
Many of the concepts of such popular third-party ORM frameworks were incorporated in JPA. Just as JDBC provides uniform APIs for accessing relational databases, JPA provides uniform APIs for accessing ORM libraries. Third-party ORM frameworks provide implementations of JPA on top of their own framework. The JPA implementation may use the JDBC APIs underneath.
We will explore many features of JDBC and JPA in this chapter as we build applications using these frameworks. In fact, we will build the same application, once using JDBC and then using JPA.
The application that we are going to build is for student-course management. The goal is to take an example that can show how to model relationships between tables and use them in JEE applications. We will use a MySQL database and Tomcat web application container. Although this chapter is about database programming in JEE, we will revisit some of the things we learned about JSTL and JSF in Chapter 2, Creating a Simple JEE Web Application. We will use them to create user interfaces for our database web application. Make sure that you have configured Tomcat in Eclipse as described in Chapter 2, Creating a Simple JEE Web Application.
We will cover the following topics:
  • Core JDBC concepts
  • Using JDBC to access the database
  • Using JDBC connection pool
  • Core JPA concepts
  • Using JPA to map entities (classes) to tables in the database
  • Configuring relationships between JPA entities
Let's first create a database and tables for this application.

Creating database schema

There are many ways of creating database tables and relationships in MySQL:
  • You can use data description language (DDL) statements directly at MySQL Command Prompt from the Terminal
  • You can use MySQL Workbench and create tables directly
  • You can create an entity-relationship diagram in MySQL Workbench, export it to create a DDL script, and then run this script to create tables and relationships
We will use the third option. If you just want to get the script to create tables and want to skip creating the ER diagram, then jump to the Script to create tables and relationships section of this chapter.
If you have not already installed MySQL and MySQL Workbench, then refer to Chapter 1, Introducing JEE and Eclipse, for instructions:
  1. Open MySQL Workbench. Select the File | New Model menu. A blank model will be created with the option to create ER diagrams:
Figure 4.1: Creating a new MySQL Workbench model
  1. Double-click the Add Diagram icon; a blank ER diagram will be opened:
Figure 4.2: Creating a new ER diagram
  1. By default, the new schema is named mydb. Double-click on it to open properties of the schema. Rename the schemacourse_management:
    Figure 4.3: Renaming the schema
    1. Hover over the toolbar buttons on the left-hand side of the page, and you will see tool tips about their functions. Click on the button for a new table and then click on the blank page. This will insert a new table with the name table1. Double-click the table icon to open the Properties page of the table. In the Properties page, change the name of the table to Course:
      Figure 4.4: Creating a table in ER diagram
      1. We will now create columns of the table. Double-click on the first column and name it id. Check the PK (primary key), NN (not null), and AI (auto increment) checkboxes. Add other columns as shown in the following screenshot:
        Figure 4.5: Creating columns in a table in the ER diagram
        1. Create other tables, namely Student and Teacher, as shown in the following screenshot:
          Figure 4.6: Creating additional tables
          Note that if you want to edit column properties of any table, then double-click the table in the ER diagram. Just selecting a table by a single click would not change the table selection in the Properties page. All columns in all tables are required (not null), except the last_name column in Student and Teacher tables.
          We will now create relationships between the tables. One course can have many students, and students can take many courses. So, there is a many-to-many relationship between Course and Student.
          We will assume that one course is taught by only one teacher. However, a teacher can teach more than one course. Therefore, there is a many-to-one relationship between Course and Teacher.
          Let's now model these relationships in the ER diagram:
          1. First, we will create a non-identifying relationship between Course and Teacher.
          2. Click on the non-identifying one-to-many button in the toolbar (dotted lines and 1:n).
          3. Then, click on the Course table first and then on the Teacher table. It will create a relationship as shown in Figure 4.7. Note that a foreign key Teacher_id is created in the Course table. We don't want to make a Teacher_id field required in Course. A course can exist without a teacher in our application. Therefore, double-click on the link joining Course and Teacher tables.
          4. Then, click on the Foreign Key tab.
          1. On the Referenced Table side, uncheck the Mandatory checkbox:
            Figure 4.7: Creating a one-to-many relationship between tables
            Creation of a many-to-many relationship requires a link table to be created. To create a many-to-many relationship between Course and Student, click on the icon for many-to-many (n:m) and then click on the Course table and Student table. This will create a third table (link table) called Course_has_Student. We will rename this table Course_Student. The final diagram is as shown in the following screenshot:
            Figure 4.8: ER diagram for the course management example
            Follow these steps to create DDL scripts from the ER diagram:
            1. Select the File | Export | Forward Engineer SQL Create Script... menu.
            2. On the SQL Export Options page, select checkboxes for two options:
              • Generate DROP Statements Before Each CREATE Statement
              • Generate DROP SCHEMA
            3. Specify the Output SQL Script File path if you want to save the script.
            4. On the last page of the Export wizard, you will see the script generated by MySQL Workbench. Copy this script by clicking the Copy to Clipboard button.

            Script to create tables and relationships

            The following is the DDL script to create tables and relationships for the course management example:
            -- MySQL Script generated by MySQL Workbench -- Sun Mar 8 18:17:07 2015 -- Model: New Model Version: 1.0 -- MySQL Workbench Forward Engineering SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; -- ----------------------------------------------------- -- Schema course_management -- ----------------------------------------------------- DROP SCHEMA IF EXISTS `course_management` ; -- ----------------------------------------------------- -- Schema course_management -- ----------------------------------------------------- CREATE SCHEMA IF NOT EXISTS `course_management` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ; USE `course_management` ; -- ----------------------------------------------------- -- Table `course_management`.`Teacher` -- ----------------------------------------------------- DROP TABLE IF EXISTS `course_management`.`...

            Table des matiĂšres