Distributed Systems Course Material Home
Create Database
In this next step we will create a middle tier that connects with the Servlet and interacts with a database. The database is where our data will be stored. The first step is to create the database.
Scripts named Create_Turtle_tracker.txt and Load_sample_data.txt have been provided (see below video). Follow the video steps to create the database and load sample data.
Generate Entity Classes
In most enterprise wide software architectures there are objects that represent the entities in the domain. So, if it was an accounting system, the entities could be customer, account, transaction, deposit, etc. Different architectures and technologies give these types of objects different names, but they all follow the basic concept.
Luckily for us, Netbeans can generate some basic entity objects by reading the database. So, it can actually read the database that we just created and create entity classes.
Generate Session Classes
If entity classes represent that entities of the application and Servlet/JSPs are the user interface, we are still missing one important piece. We need code to represent the user session. These session classes interact between the user interface, business logic and entities.
Once again, we are lucky that NetBeans will auto-generated simple session classes for us.
Note: If you see this error, “Cannot be generated because EJB Lite classes are not available on project classpath”, confirm that you have included the Jakarta EE 8 API Library by right-clicking on the project | Properties | Libraries. Then restart Netbeans.
Modify JSP and Servlet
Now we can have our Servlet/JSP take the data entered by the user, call the session class, which in tern uses the entity class to store the information in the database.
In your Servlet close to the top add this to your import area. Just copy and paste it.
import entity.*;
import session.*;
import javax.persistence.*;
import javax.transaction.*;
import javax.annotation.*;
import javax.enterprise.context.*;
import javax.ejb.*;
Right below here is says public class SpecimenServlet in the servlet add:
@EJB
private SpecimenFacadeLocal SpecimenSession;
Then in your Servlet there is a method called processRequest. Just below were we did a standard output (print) to the console with the user entered data, we put the following code.
Specimen sp = new Specimen();
sp.setSpecimenId(10);
sp.setSpecimenWeight(Integer.parseInt(request.getParameter(“weight”)));
sp.setSpecimenLength(Integer.parseInt(request.getParameter(“length”)));
sp.setSpecimenHealth(request.getParameter(“health”));
sp.setBreedCode(request.getParameter(“breed”));
sp.setLocationCode(request.getParameter(“location”));
SpecimenSession.create(sp);
Watch here.
In the above example the SpecimenSession we use the create method which inserts a row into the database table. But other important methods on the SpecimenSession object are:
- Methods find, findAll and findRange. This will return one or more Specimen objects. These methods can be used to retrieve data from the database.
- The remove method will delete a record from the database by passing in the Specimen object to be deleted.
- The edit method will update a record in the database. Retrieve the Specimen object to be updated, change the attributes and then edit it.
Make Some Configuration Changes
There are some minor configuration changes that we need to make.
Under the project properties, go to the libraries tab and make sure that the following libraries are included.
- Jakarta EE 8 API Library
- EclipseLink (JPA 2.0)
- Persistence (JPA 2.0)
- EclipseLink from GlassFish
In the project’s persistence.xml file change the transaction-type to “JTA”.
In the GlassFish Admin Console, under JDBC | DerbyPool | Additional Properties tab, make the following changes.
- User: TTS
- Password: TTS
- DatabaseName: TurtleTracker
Testing
Now, we can test it.
Troubleshooting
Java DB database will not start.
- This can happen if when you installed NetBeans it automatically selected another Java environment to use. In some cases the version of Java DB that we are using will not work with newer versions of Java. Find the Netbeans.conf file. This is probably in your Netbeans /etc folder. Find the line that begins with netbeans_jdkhome=. Make sure that it is set to the location of your Java 17 installation. It not, change it and restart Netbeans. To know what Java version Netbeans is using look on the About page [Windows: click on Help | About: MAC click on NetBeans | About.]
Get an error when the Servlet tries to save the data.
- Go back into the Glassfish Admin Console and confirm that the DatabaseName is set to TurtleTracker.