JPA in Eclipse and MySQL. Simple example: Main program to write and read a database table

I show you how to create a JPA program in Eclipse accessing a simple MySQL Employee table. A main method writes a new record to that table, then retrieve all records. No internet drivers, no web server stuff, no Java EE. Basic example. (No Hello World!!!)

Assumes you know how to create a:
– database in MySQL, and you have one already created.
– connection to that database in Eclipse, and have done so.
– table in it: (I give you SQL statements which I use in phpmyadmin)
– (driver for MySQL is assumed to be working, and I show you how to download and include EclipseLink driver; no other dependencies!!! Yay!)

Tutorial Eclipse JPA MySQL:

1. mysql: create and populate a table:

CREATE TABLE employees (
name VARCHAR(30),
started INT

INSERT INTO employees (name, started) VALUES
(‘John Smith’, 2014),(‘Mary Doe’, 2017),(‘Frank Jones’, 2016);

SELECT * FROM employees;


2. (make a connection in Eclipse to this)

3. New JPA project/ Project name: jpa-eclipse-mysql / Next/Next/Platform: EclipseLink 2.5.x/ JPA Implementation: Type: User Library/ EclipseLink 2.5.2 (you may have to click the little “download library” icon to the right/ Connection (select the connection that you created in step 2)/ Add driver library to build path/Finish

4. New/JPA Entities from Table/ Select Employees table/ Next/ Next (skip the Table Associations stuff)/ Key generator: Identity/ Package:mystuff/Finish

5. Create with (Note that, in this file, I’m using jpa-eclipse-mysql because when I created the project with that name, Eclipse generated that same name in persistence.xml (persistence unit name):
package mystuff;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class EMF {
private static final EntityManagerFactory emfInstance =

private EMF() {}

public static EntityManagerFactory get() {
return emfInstance;

6. create
package mystuff;

import java.util.List;
import javax.persistence.*;

public class MyMain {

public static void main(String[] args) {


EntityManager em = EMF.get().createEntityManager();

Employee employee = new Employee();
employee.setName(“Tom Stevens”);

System.out.println(“writing newly created object as a database record…”);
// store current one
EntityTransaction tx = em.getTransaction();
System.out.println(“one written.”);

System.out.println(“\nretrieving all records…”);
Query q = em.createQuery(“select e from Employee e”);
// type safety warning left as an exercise for you:
List<Employee> lis = q.getResultList();
System.out.println(“found:” + lis.size());
for (Employee e: lis) {
System.out.println(“found:” + e.getId() + ‘ ‘ + e.getName() + e.getStarted());
System.out.println(“all retrieved.”);



7. Add your database connection info to properties in persistence.xml so it has 4 new properties 1. the driver, 2. the database name 3. the username for that database 4. the password. Mine now says:
<?xml version=”1.0″ encoding=”UTF-8″?>
<persistence version=”2.1″ xmlns=”; xmlns:xsi=”; xsi:schemaLocation=””&gt;
<persistence-unit name=”jpa-eclipse-mysql”>

<property name=”javax.persistence.jdbc.driver” value=”com.mysql.jdbc.Driver” />
<property name=”javax.persistence.jdbc.url” value=”jdbc:mysql://localhost:3306/flashcardsquiz” />
<property name=”javax.persistence.jdbc.user” value=”flashcardsquiz” />
<property name=”javax.persistence.jdbc.password” value=”password” />


8. Right click on, run as a java application. Here’s my output:

[EL Info]: 2017-10-15 20:21:53.677–ServerSession(351962798)–EclipseLink, version: Eclipse Persistence Services – 2.5.2.v20140319-9ad6abd
Sun Oct 15 20:21:54 EDT 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended.
Sun Oct 15 20:21:54 EDT 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended.
[EL Info]: connection: 2017-10-15 20:21:54.678–ServerSession(351962798)–file:/jpa-eclipse-mysql/build/classes/_jpa-eclipse-mysql login successful
writing newly created object as a database record…
one written.

retrieving all records…
found:1 John Smith2014
found:2 Mary Doe2017
found:3 Frank Jones2016
found:4 Tom Stevens2017
all retrieved.

If you found this useful, you might like to leave a thanks in a comment below. Share the luv! 🙂


Posted in Eclipse, java, jpa, Uncategorized | Tagged , , | Leave a comment

Installing Eclipse with Java 9. Minimum barebones Eclipse IDE for Java EE Developers. Fast. Eclipse Oxygen 1a

Eclipse IDE is slow. Java 9 is now supported by Eclipse Oxygen. I show you how to install a minimum version of Eclipse IDE for Java EE Developers, without the bloat, and fast!!!

This tutorial:

  • is NOT for Eclipse beginners, it’s for developers.
  • assumes linux
  • assumes you have Tomcat downloaded and unzipped and not installed (using any package manager) and not running.
  • creates a very simple servlet and a web page.

Download Oracle Java 9 JDK

1. Download JDK from the Oracle download site and save the download to your Downloads directory:


Oracle JDK download site

Download Eclipse IDE Installer

3. Download the Eclipse Installer linux 64 bit and save the download to your Downloads directory:


Eclipse IDE linux 64 bit installer download

4. Rename the existing Eclipse settings directory. Rename the profiles directory. (We want a brand new environment):

$ cd ~
$ ls -lart | grep eclipse
drwxrwxr-x   10 louie louie   4096 Sep 23 18:00 .eclipse
$ mv ~/.eclipse ~/.oldeclipse
$ ls -lart | grep eclipse
drwxrwxr-x   10 louie louie   4096 Sep 23 18:00 .oldeclipse

$ ls -larth | grep p2
drwxrwxr-x    6 louie louie 4.0K Oct 12 15:17 .p2
$ mv .p2 .oldp2
$ ls -larth | grep p2
drwxrwxr-x    6 louie louie 4.0K Oct 12 15:17 .oldp2

5. Go to ~/Downloads, uncompress the Eclipse download into ~/Downloads/:

$ cd ~/Downloads
$ ls -lart eclipse-inst*
-rw-rw-r-- 1 louie louie 48022873 Oct 11 16:13 eclipse-inst-linux64.tar.gz

$ tar -xzf eclipse-inst-linux64.tar.gz

$ ls -lart | grep eclipse-inst
drwxr-xr-x   7 louie louie      4096 Jun 14 02:38 eclipse-installer
-rw-rw-r--   1 louie louie  48022873 Oct 11 16:13 eclipse-inst-linux64.tar.gz

6. Also in ~/Downloads, uncompress the JDK download into ~/Downloads/:

$ cd ~/Downloads
$ ls -larth | grep jdk-9
-rw-rw-r--   1 louie louie 331M Oct 11 17:27 jdk-9_linux-x64_bin.tar.gz
$ tar -xzf jdk-9_linux-x64_bin.tar.gz
$ ls -larth | grep jdk-9
-rw-rw-r--   1 louie louie 331M Oct 11 17:27 jdk-9_linux-x64_bin.tar.gz
drwxrwxr-x   8 louie louie 4.0K Oct 11 18:15 jdk-9

Remaining steps

  1. Add Java’s bin directory to the path:

    $ echo 'export PATH=~/Downloads/jdk-9/bin:$PATH' >> ~/.bashrc
    $ source ~/.bashrc
    $ jshell
    |  Welcome to JShell -- Version 9
    |  For an introduction type: /help intro
    jshell> /exit
    |  Goodbye
  2. Start the install of Eclipse:

    $ cd eclipse-installer
    $ ./eclipse-inst
  3. After the installer program starts, click the icon to update the installer (top)

  4. Click on Eclipse Platform (absolute minimum IDE) and accept the suggested directory

  5. Help/Install New Software/All Available Sites:

    -Eclipse Java EE Developer Tools

    -Eclipse Java Web Developer Tools

  6. Install: JST Server Adapter Extension (just this one!)

  7. Window/Perspective/Open Perspective/Other/Java EE

  8. Window/Show View/Other/Problems

  9. File/New/Dynamic Web Project/myproject/Finish

  10. Right click on myproject/New/Servlet/Java package:mystuff/Class name:MyServlet/Finish

  11. Right click on myproject/Properties/Java Build Path/Classpath/Add External Jars/servlet-api.jar (from your Tomcat9 folder)/Ok/Apply and Close

  12. (Optional)Markers Tab (at the bottom of the screen)/(expand)Classpath Dependency Validator Message/Right click on Classpath Entry/Quick fix/Finish

  13. Right click on myproject/Run as/Run on server/(manually define a new Apache/Tomcat9 server)/Finish (shows error page)

  14. Point Firefox/Chrome to http://localhost:8080/myproject/MyServlet (and smile!)

  15. Find WebContent folder in the project, right click on it/New/HTML File/index.html/Finish

  16. Insert your hello world content into that page, and save.

  17. Visit your masterpiece at http://localhost:8080/myproject/

  18. If you found this useful, leave a thanks in the comments below. Share the luv.

Posted in Eclipse | Tagged , | Leave a comment

Simple jax-rs java restful rest eclipse maven example

After failing to get anything running from “Restful Java with JAX-RS 2.0”, you need to see if you can get the minimal setup running. Here’s how.

New dynamic web project.  Project name: rest   Finish

Right click rest in project explorer // Configure // convert to Maven project // Finish.

pom.xml should show in the window.  At the bottom of the window, click the tab for “pom.xml” so that you can edit the xml source code:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Right click on the rest project in project explorer.  New// class//package mystuff // Name Timezone// Finish

Enter this stuff:

package mystuff;


public class Timezone {

	public String timezone() {
		return "time to party";

	public String myfunction(@PathParam("myverb") String myverb) {
		return "{\"time\" : \"" + myverb + "\"}";

Find the folder WebContent/WEB-INF, right click on it,


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:web="" xmlns:xsi="" xsi:schemaLocation="" version="3.0">

right-click the rest project in Package explorer.  Run as // Maven install

right-click the project rest and run as // run on server // Tomcat 9

Go to Chrome/Firefox and enter the following URL:


time to party

Now enter this url:


{"time" : "sing"}

Next steps:

1. create with path of “/” showing a welcome message to the site, and output it in HTML.

2. Create a new method in the Timezone class that accepts POST requests.

I didn’t implement POST, because the goal is to get something running, and implementing POST before GET is a huge drain on your time. With GET, you can experiment much easier with a browser.

To test your POST, install Postman in Chrome. I use restlet client in Chrome myself, it’s more lightweight.

A good book on how to write APIs correctly is “Rest API Design Rulebook”. I didn’t follow its rules here, it wasn’t my objective. I took a few shortcuts too, like using * with imports, which is a bad thing.

I was shocked after reading the Java JAX-RS book how much of a complicated example he made right at the start. I thought it’d be a huge hurdle to learn enough to get something basic running, but this example proves otherwise. Too bad he didn’t start off with an example like this. (You can use that idea, Bill Burke!)

If you found any of this helpful, share the luv, leave a thanks below 🙂

Posted in Uncategorized | Leave a comment

Spiritual Development of the World: Focus on the Change

I was inspired to change things. Today. Now. Maybe from now on.

It might be nice to spend my life being of genuine use to people. It might be nice to forget the day-to-day trivialities, the pettiness in the world, the news, and concentrate on helping people effect changes in their spiritual development.

Why have I been posting about computer stuff? I could be posting about real stuff.


So I have 2 stories. An inspirational web site, and my own experience jamming with friends.

1. Heather MacDonald’s Facebook page is inspiring.  I’d love to have a Facebook page just like that…  Totally oriented towards spiritual growth and growth in others; no silly postings about stuff that doesn’t matter.  Maybe I’ll start now.  Maybe.  Turns out we went to the same schools and moved to Ottawa. Here’s her post:

A year from now, everything you are stressing about won’t even matter […]

What matters is making the music, not the state of the nation

2. The other is my jam session on Saturday with some friends.  I’ve not been able to leave the house much lately, and I made the effort, but it was moving.  We did stuff that was important.  We made music with each other.  I went home and thought about how I hear from people their gripes with politics, the state of the world, and I think how little this matters.  What matters is making the music.

I don’t know why,  but it’s the same when you write, make art, sing, dance, draw.  If you can create, you really should create.  Spend less time on the trivial.  Less time on the petty.  Do it now.  Stop reading.  Write a few lines on your blog or your social media. Right now.

What would it be like if my social media from now on consisted of this formula?: If it’s not inspirational, and not helpful to others’ spiritual growth, I won’t post it; and I won’t “like” it.

Practice (music/art): accretion of time over the long run by daily practice is far better than “spurts” approach

It seems to take a huge investment in time to become even moderately good in some things.

Like piano practice, or reading a new book, we may approach it with enthusiasm, commit ourselves to spending 3 hours a day with it, but what happens?  We fizzle out.  I’ll find myself web surfing or doing some silly semi-related task, but not practicing.

Turns out that we might benefit by doing something sustainable, like 10 or 15 minutes a day.  That way, on tough days we are looking only at “I have to do this for 10 minutes, and so it’ll be over with quickly”

“binge” writers write between 2 and 12 hours in one day, but then do not return to writing for a week or more verses more regular writers who write for short periods but write every day) […] One assumption is that writers are most productive if they write when the mood hits … they can spend hours of uninterrupted time at the task, i.e. “binge writing.”. Conclusion: Daily writers are more productive than binge writers and generated more creative ideas for writing than did subjects who wrote spontaneously” Challenge Common Assumptions Against Daily Writing (2012) Dr. Sally

Ten minutes a day is sustainable, and the accretion of time over the long run by daily practice is far better than “spurts” approach.

I meditate each morning for 8 to 10 minutes.  Many days I really, really don’t want to.   But it is such a small investment.

The first two hours of the day are the most productive.  Don’t waste them.

Posted in Uncategorized | 1 Comment

Our TRUE Nature vs. Our Experiences

We have a higher self that’s always been there and is there right now, but it is easy to lose sight of this and be overwhelmed by current events and feelings, and go through life that seems to be run by our past experiences, and not even have an awareness of this higher self.

I think my current job in life is to become aware of my “nature”; this nature that was there before I acquired any human experience. It will always be there. My job seems to be to learn about this nature (my true/higher self), and to identify with this nature, rather than identifying with my human experiences (experential self ? ). There has been much in the 20th century that said we are the product of our experiences, but I’m challenging this.

Take an experience where you felt care-free or spontaneous as a child, and/or did things that were fun and joyful. And then over time, life happened. But even after life’s crises, you may have had moments, as an adult, where you felt for brief moments that child-like nature. Perhaps that is a manifestation of my timeless nature. My real self. It’s always been there.

It’s very easy for me to have my life controlled by my past experiences, if I let it. And I think I seem very willing to let it. I used to have conversations with total strangers, and within three minutes, they would hear something about the wrongs that were done to me in the past. Obviously, I was re-living these experiences again and again, and they became so deeply rooted in me, they took over my identity, with no hope whatsoever of seeing any future with anything new or changeable.

There’s a perverse pleasure from identifying with our past pain. It’s similar to the perverse pleasure I get from self-pity. Both of these, in addition, free me from the need to take responsibility for my life and take steps to effect changes.

Here’s a quote from my video of the week by Eckhart Tolle.

[There are] two things getting together. Old pain, that wants more pain, and and the mind made self that needs to be right, and therefore needs to make somebody else wrong. “He did something to me”. […] We miss the simplicity of things [such as a typical daily event that just happened,] that is there without the story. [The story is the drama we add to day-to-day events by adding in our past pain, and making the innocuous event more personalized (ie, more about me), and explaining the event in terms of our past painful experiences].

So Tolle’s suggestion was to just witness the pain we are feeling, not even need to label it, certainly don’t analyze it or add stories to it. Just witness it.

Current pains relate to this blog post because we so easily identify with our pain. This means that our real selves are once again obscured by the pain we are in right now. And for the moments of the pain, we are overwhelmed with our past experiences, including fears and hurts, and create much more misery by adding in those elements. It’s easier to just experience the pain, no need for the drama. No need to say thing like “This always happens to me”, or “well I expected as much”.

My last blog post was about living in faith, regardless of what my past experiences are leading me to believe. That is, despite me thinking certain current events are explainable because of my past (painful) experiences, and I have expectations that the future will bring certain outcomes because I’ve learned to expect these outcomes, I can choose to not let these thoughts dominate my life. And I can live without acting in ways that are controlled by my experiences. That is, I can choose to live in faith, and do things, despite what my mind is trying to tell me, and be able to do that because of a faith that things will be OK. It opens up the world for new and wonderful possibilities.

P.S. [from my bathtub reading today, just an interesting tidbit,  Jan 25th]:

The ego wants to be right, but in the dynamics of life and art we are never right, we are always changing and cycling. [Free Play, Improvisation in Life and Art, Stephen Nachmanovitch].

My recent blog posts:

Depression, Procrastination and Putting One Foot in Front of Another

High Expectations of Admired Friends

Depression Paradoxes: Isolation

Afternoon Forest Walk

Posted in Uncategorized | Leave a comment

Total Faith, Let Go, and Live as If

What a tall order to totally surrender control of your life, let go of your plans and your ideas of what is best for you, let go of your fears based on years of programming in which you see imaginary futures, and just live totally in the present.

How can I be present with someone, and let God’s will take place if my mind is on my fears of the future based on my own perceived inadequacies?  I’ve already determined the outcome (in my mind), because I’ve learned to have certain expectations of what has “always happened” in the past.  It probably hasn’t “always happened”, but I seem unwilling to let this notion go, and therefore I prevent any possibility of anything happening which may be different from what I’ve learned to expect.  So if I expect disappointment, I’ll behave in a way that prevents me from living fully and, therefore is a threat to anything novel, which, chances are, will not be a disappointment.

I can’t love totally if I’m planning outcomes.  Of course, the outcomes I’m speaking of are the negative outcomes that we keep telling ourselves; messages we have been telling ourselves for decades.

Did you ever talk to somebody, and be looking out the corner of your eye at someone else who, in your infinite wisdom, is necessary to talk to right now, or as soon as possible.  I did this a lot.  I don’t anymore.  I have been trying to be fully present.  Ideally, if I wasn’t supposed to be fully immersed in the person in front of me, and ideally, if I had faith, I’d know that my higher power wouldn’t have this person in front of me, and, instead, he would put the other person in front of me.  “You are always exactly where you need to be at this moment in time”.  How little faith we have.  Even if we say we have faith in that statement, our mind is thinking of a million scenarios which if we do this or do that, we can make a certain outcome happen for us.  AS IF WE KNOW BETTER WHAT’S GOOD FOR US!

My mind may be useful for some things (less and less as time goes by), but for planning and scheming my future based on what I think is best, is definitely not one of them.

So I had an amazing experience of living in faith and being totally present with someone, and even though my mind was on my inadequacies and expectations that this will soon blow up in my face because they will not be OK with me exactly as I am and at this point in time.  But I was wrong, very wrong.  And seeing this is totally foreign to me, I mean seeing that I don’t have to be anyone or do anything or plan anything or change anything whatsoever in order to be accepted totally.  I’ve learned to realize that if I’m me and not trying to be something else, not even hinting at it, and if I live in faith that I will be accepted exactly as I am, wherever I am at my journey, it’s the only way to find real love.  But it takes a huge leap of faith.  Huge.  It’s almost impossible to do it, but it is possible.  I think the key is, just behave as if you have total faith in this idea, regardless of all the messages and planned futures (of negativity) that are in your head.


Posted in Uncategorized | 1 Comment

Second JSF Tutorial with Eclipse, Tomcat

Using JSF with Eclipse, we create a java class used to navigate to a second xhtml page and also create that second xhtml page.

Previous tutorial was here

  1. Modify the index.xhtml to include this the code as listed below.
  2. Create page2.xhtml in the same folder as index.xhtml and use the code below.
  3. Create a class by right-clicking on the project name (JsfTut)/New class.  Name it and include the code below
  4. Right click on the project (JsfTut), and run on server.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
<h1>Congrats, this works!</h1>

Let's go to a new page:
<h:commandButton value="Click for page 2"



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
<h1>This is page 2 :)</h1>


import javax.faces.bean.ManagedBean;

public class MyPageClass {

 public String nextPage() {
 return "page2";

Found this useful?  Share the luv, and leave a comment 🙂

Posted in Uncategorized | Leave a comment