- Java SE JDK 8 (Link)
- Eclipse Neon for Java EE Developers (Link)
- Glassfish 4 Full (Link)
- SQLite 3 (Link)
- SQLite JDBC (Link)
- SQLiteConnectionPoolDataResource (Link)
Download all programs to a safe location and setup Glassfish to be usable in Eclipse (using Glassfish Tools from the Eclipse Marketplace) and SQLite to be accessible via command-line.
Open SQLite3 inside the command-line. Then type .save mydatabase.db
to save the (currently empty) in-memory database to a file called mydatabase.db
. This file will be used by Glassfish and can be inspected using SQLites command-line utility.
- Copy
sqlite-connection-pool_v1.2.0.jar
andsqlite-jdbc-3.18.0.jar
into[path to glassfish]/glassfish4/glassfish/lib
. - Start Glassfish (e.g. inside Eclipse) and open
http://localhost:4848/
(Glassfishs admin page) inside your browser. - Navigate to
Resources > JDBC > JDBC Connection Pools
and clickNew
. - Enter:
- Pool Name:
SQLite
- Resource Type:
javax.sql.ConnectionPoolDataSource
- Datbase Driver Vendor (below the drop-down menu):
SQLiteJDBC
- Click
Next
- Pool Name:
- Enter:
- Datasource Classname (below the drop-down menu):
org.sqlite.SQLiteConnectionPoolDataSource
- Click
Finish
- Datasource Classname (below the drop-down menu):
- Click on the newly created SQLite connection pool and navigate to the
Additional Properties
tab (above the page). - Click
Add Property
and enter:- Name:
url
- Value:
jdbc:sqlite:[path to your database file]/mydatabase.db
- Click
Save
- !Caution! On windows, the path has to include the drive letter and must use forward slashes instead of backslashes.
- Name:
- Now navigate to
Resources > JDBC > JDBC Resources
and clickNew
. - Enter:
- JNDI Name:
jdbc/SQLite
- Pool Name:
SQLite
- Click
OK
- JNDI Name:
- Close the admin page, restart the server and check, whether everything you just created is still there.
- It didn't save properly for me once, so in case of unexpected errors, check again.
- If everything looks as expected you are done and can proceed to use the database in a project.
- These steps are also shown with pictures on: How To Setup a SQLite Connection Pool on Glassfish
- They use an older version of Glassfish, but it still looks very similar!
- Create a new Dynamic Web Project in Eclipse.
- Right click the project, navigate to
Configure
, clickConvert to JPA Project...
and clickNext
andFinish
. - Right click the project again and navigate to
Properties > JPA
and enter in the drop-down menu belowCanonical Metamodel
thesrc
folder. - Now open the project and the
JPA Content
subfolder and open thepersistence.xml
. - Open the
Source
tab and enter the contents of this persistence.xml. - It should now be possible to inject an
EntityManager
using annotations and access the database.- When injecting the
EntityManager
using@PersistenceContext
, you can also provide the unit nameglassfish-sql
from thepersistence.xml
. - It would look like
@PersistenceContext(unitName="glassfish-sqlite") EntityManager em;
or similar.
- When injecting the