B0B36DBS, BD6B36DBS: Database Systems h p://www.ksi.m.cuni.cz/~svoboda/courses/172-b0b36dbs/ Prac cal Class 10 JDBC, JPA 2.1 Author: Mar n Svoboda, mar n.svoboda@fel.cvut.cz Tutors: J. Ahmad, R. Černoch, M. Řimnáč, M. Svoboda, G. Šourek 24. 4. 2018 Czech Technical University in Prague, Faculty of Electrical Engineering
JDBC Java Database Connec vity
First Steps Create a new NetBeans project Select Java Applica on as a project type Create a main class Include method Add PostgreSQL JDBC Driver library Projects window your Project node Libraries context menu Add Library item B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 3
Database Connec on Dynamically load the JDBC driver for PostgreSQL Open a new database connec on Close the database connec on B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 4
Table De ni on Create a schema for a table of books Include the following columns : book iden er, integer, primary key : book tle, varchar Insert sample data into our table of books B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 5
Data Querying Select and process all books Print book iden ers and tles to the standard output B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 6
Prepared Statements Retrieve books based on their tles Use a prepared statement for this purpose B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 7
JPA 2.1 Java Persistence API
First Steps Extend our NetBeans project Create a new main class Include method Make this class the main class of the project Projects window your Project node context menu Proper es item Run sec on Main Class eld Add EclipseLink JPA 2.1 library Projects window your Project node Libraries context menu Add Library item B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 9
Database Connec on Con gure your database connec on in NetBeans Create and con gure a new connec on Services window Databases node context menu New Connec on item Driver: PostgreSQL Host: Port: Fill in your Database, User name, and Password B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 10
Persistence Unit Create and con gure a new persistence unit I.e. add a new le into your project Projects window your Project node context menu New item Other subitem Category: Persistence File type: Persistence Unit Con gure the persistence unit Set Persistence Unit Name Select your Database Connec on Table Genera on Strategy: Create Browse XML source code of your persistence unit B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 11
En ty De ni on Create a new en ty for books I.e. add a new Java class into your project Include the following a ributes : book iden er, integer, primary key : book tle, string : auxiliary comment, string B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 12
En ty Annota ons Use annota ons to make sure that Class Book is treated as a database en ty book2 is a name of the associated table Values of id column are automa cally generated book tle is a name of column for book tles values are not permi ed for book tles A ribute comment is not included in the database B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 13
En ty Registra on Register the book en ty with our persistence unit persistence.xml le Design view General sec on Include En ty Classes list Add Class bu on B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 14
En ty Manager Create a new instance of an en ty manager factory Create a new instance of an en ty manager Close the database connec on B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 15
Data Querying Retrieve a book according to its iden er Print book iden er and tle to the standard output B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 16
JPQL Statements Retrieve and process all books Print book iden ers and tles to the standard output B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 17
Parameterized Queries Retrieve a book using a parameterized query B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 18
En ty Transac ons Ini alize a new en ty transac on B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 19
En ty Manipula on Create a new book en ty Update the previous book en ty Remove the previous book en ty B0B36DBS, BD6B36DBS: Database Systems Prac cal Class 10: JDBC, JPA 2.1 24. 4. 2018 20