Part 1: Install JDK, MySQL, and NetBeans
Part 2: Build and run a JPA application that persists a Book entity
By the end of this lab, you will have a working development environment and a running Java application that writes data to a MySQL database using JPA — without writing a single line of SQL.
Install and configure the three tools you'll use for the rest of this unit: JDK 21 (the compiler), MySQL (the database), and NetBeans (the IDE). You'll verify each installation before moving on.
Create a Java application that defines a Book entity, configures JPA with EclipseLink, connects to MySQL, and persists a book record. You'll see the data appear in your database.
Installing and configuring JDK, MySQL, and NetBeans — the foundation for every project in this unit.
Each tool serves a specific role in the Jakarta EE stack. Understanding why you need each one helps when troubleshooting later.
Go to oracle.com/java/technologies/downloads/#jdk21-windows and download the .exe installer for JDK 21. Run it with default settings.
Open Control Panel → System → Advanced System Settings → Environment Variables. Create the variables listed in the table to the right.
Open a new Command Prompt (existing ones won't see the new variables) and run the two commands below.
| Variable | Value |
|---|---|
| JAVA_HOME | C:\Program Files\Java\jdk-21 |
| JDK_HOME | C:\Program Files\Java\jdk-21 |
Then add to the PATH variable:
JAVA_HOME to locate the JDK. Without it, they can't find the compiler.
Go to dev.mysql.com/downloads/installer/ and download the community installer. Run it.
You need exactly three components. In the product selection screen, add all three to "Products To Be Installed" before proceeding.
Install as a Windows Service (so it starts automatically). Set the root password — remember this, you'll need it in Part 2. Optionally create a user account (e.g., username: APP).
Open MySQL Workbench, connect to your local instance, and confirm you can see the query editor. Also check Task Manager → Services tab to confirm MySQL80 shows "Running".
| MySQL Server | The database engine |
| MySQL Workbench | GUI for managing databases |
| Connector/J | JDBC driver for Java apps |
C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.29.jarGo to netbeans.apache.org/download/index.html and download the .exe installer for NetBeans 20 or higher. Run it with default settings.
Launch NetBeans. You should see the Start Page with no error messages. If NetBeans cannot find the JDK, it will show an error — this means your JAVA_HOME is not set correctly.
JAVA_HOME setting from Step 1.
java -version shows JDK 21javac -version shows 21.0.xIf something isn't working, check here before asking for help.
Your PATH variable doesn't include the JDK bin directory. Re-open Environment Variables and make sure %JAVA_HOME%\bin is in PATH. Then open a new terminal — existing terminals don't pick up changes.
Check if another MySQL instance or another program is using port 3306. Open Task Manager → Services and look for conflicting services. You can also try restarting the MySQL80 service manually.
Make sure JAVA_HOME points to the JDK directory (not the JRE). The path should end with \jdk-21, not \jre. You can also configure the JDK path in NetBeans via Tools → Java Platforms.
Verify MySQL service is running. Check that your root password is correct. If you get "Access denied", reset the password through the MySQL Installer's reconfigure option.
1. Why do we set the JAVA_HOME environment variable?
2. Which MySQL component allows a Java application to connect to the database?
3. After setting PATH, you open a terminal and type java -version but get "not recognised". What's the most likely fix?
Building a Java application that persists a Book entity to MySQL using JPA and EclipseLink — no SQL required.
A simple Java SE application that creates a Book object in memory and persists it to a MySQL database table using JPA. Here's the full picture of what's involved:
eclipse.dev/eclipselink/#download and extract to a folderHere's the final file structure of the project you're building, so you know where everything goes.
The JPA entity class annotated with @Entity. Maps to a database table. Contains fields for title, price, description, ISBN, page count, and illustrations.
The entry point. Creates a Book object, obtains an EntityManager, opens a transaction, persists the book, and commits.
Tells JPA which database to connect to (JDBC URL, username, password), which provider to use (EclipseLink), and which entity classes to manage.
In NetBeans: File → New Project → Java with Ant → Java Application. Click Next.
Project Name: W1P1. Tick "Create Main Class" and set it to w1p1.W1P1 (or w1p1.Main). Choose a project location you can find later. Click Finish.
NetBeans generates a project folder with a src/ directory containing your package (w1p1) and a Main.java file with an empty main() method. The Libraries node will initially only contain JDK 21.
Before JPA can connect, the target database must exist. JPA will create the tables for you, but it cannot create the database itself.
Click the connection tile (e.g., "Local instance MySQL80") and enter your root password.
In the query editor, run the SQL command shown on the right, then click the refresh button in the Schemas panel to confirm w1p1db appears.
| Host | localhost |
| Port | 3306 |
| Database | w1p1db |
| Username | APP (or root) |
| Password | Your MySQL password |
| JDBC URL | jdbc:mysql://localhost:3306/w1p1db |
Right-click the w1p1 package → New → Entity Class. Set Class Name to Book, Package to w1p1, Primary Key Type to Long. Tick "Create Persistence Unit".
On the next screen: Persistence Unit Name: W1P1PU. Persistence Library: EclipseLink (JPA 3.0). Database Connection: select "New Database Connection..."
Driver: MySQL (Connector/J driver). If the driver file isn't found, click Add and browse to the Connector/J JAR. Then fill in host, port, database (w1p1db), username, and password. Click "Test Connection" to verify.
persistence.xml file under META-INF/. This file is the bridge between your Java code and the database — it tells JPA where the database is and how to connect.
jlib/ folder of your extracted EclipseLink download) are added to the project's Libraries. Without them, the project won't compile.
Your Libraries node should contain:
MySQL Connector/J driver
All EclipseLink 4.0.2 JARs (eclipselink.jar, jakarta.persistence-api, jakarta.annotation-api, etc.)
JDK 21
Replace the generated Book.java content with this code. Each annotation tells JPA how to map this class to the database.
| @Entity | Marks this class for JPA management |
| @Table | Overrides the default table name |
| @Id | Marks the primary key field |
| @GeneratedValue | Auto-generates the ID value |
| @Column | Customises column name, constraints, length |
@Column (like price, isbn) still map — JPA uses the field name as the column name by default. You only annotate when you want to customise.
This is the entry point that creates a Book and persists it to the database.
Step 1: Create a Book POJO and populate its fields. At this point it's just a regular Java object — JPA doesn't know about it (it's in the "New" lifecycle state).
Step 2: createEntityManagerFactory("W1P1PU") reads persistence.xml, finds the persistence unit named "W1P1PU", and sets up the database connection.
Step 3: em.persist(book) tells JPA to manage this object. When tx.commit() runs, JPA generates an INSERT SQL statement and executes it against MySQL. The book is now in the database.
persistence.xml. A mismatch here is one of the most common errors.
This file lives in src/META-INF/persistence.xml and tells JPA everything it needs to connect to the database.
| jdbc.url | Where the database is (host, port, database name) |
| jdbc.user | MySQL username |
| jdbc.password | MySQL password |
| jdbc.driver | The JDBC driver class (from Connector/J) |
| schema-generation | create = auto-create tables on first run |
root and your password is mypassword, change them here — not in Main.java.
tx.begin() / tx.commit()). In Week 5+, we'll use container-managed transactions (JTA) where the server handles this for you.
Right-click the project → Clean and Build. The Output window should show "BUILD SUCCESSFUL". If you get errors, check that all libraries are added and the code compiles cleanly.
Right-click the project → Run. In the Output window, you should see the EclipseLink banner followed by your Book's toString() output with a generated ID.
Open MySQL Workbench, select the w1p1db schema, and run the query shown on the right. You should see one row with the book data.
**** Persist the book ****
Book{id=551, title='The Hitchhiker's Guide to the Galaxy', price=12.5, isbn='1-84023-742-2', nbOfPage=354, illustrations=false}
| ID | BOOK_TITLE | PRICE | ISBN |
|---|---|---|---|
| 551 | The Hitchhiker's Guide... | 12.5 | 1-84023-742-2 |
The ID value will vary — it's auto-generated.
w1p1db connection → Tables → myfavouritebook → right-click → View Data.
If the build or run fails, check these common problems first.
EclipseLink JARs are not in the project Libraries. Add all JARs from the EclipseLink jlib/ folder.
You forgot to create the database in MySQL Workbench. Run CREATE DATABASE w1p1db; first.
The username or password in persistence.xml doesn't match your MySQL credentials. Update the jdbc.user and jdbc.password properties.
The schema-generation action is set to create, which tries to create the table each run. Change it to drop-and-create during development, or none after the first run.
The MySQL Connector/J JAR is not in the project Libraries. Add the mysql-connector-java-8.x.x.jar file from your MySQL Connector installation folder.
Check that tx.commit() is called after em.persist(book). Without the commit, the transaction is rolled back and nothing is written.
4. What is the purpose of persistence.xml?
5. What happens if you call em.persist(book) but never call tx.commit()?
6. In persistence.xml, what does schema-generation.database.action = "create" do?
Finished early? Try these exercises to deepen your understanding. Each one builds on what you've already done.
Modify Main.java to create and persist three different books in a single transaction. Verify all three appear in the database table.
After persisting the books, use em.find(Book.class, id) to retrieve one by its ID and print it. Then try a JPQL query: em.createQuery("SELECT b FROM Book b", Book.class).getResultList() to retrieve all books.
Create an Author entity class with fields for name and biography. Add it to persistence.xml as a second <class> entry. Persist an Author and verify the new table appears in MySQL.
@ManyToOne and @OneToMany annotations. The extension tasks here give you a head start.
You now have a working Jakarta EE development environment and a running JPA application that persists data to MySQL.
If you had any issues, note them down and bring your questions to the next session.
The complete sample project is available on the Week 1 block of the unit Moodle site.