Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
42e5210
Added a simple Course class that we'll eventually persist to a database.
DavidWhitlock Dec 14, 2025
c6533f7
A simple Data Access Object for persisting Course objects to an embed…
DavidWhitlock Dec 14, 2025
c1010e2
Move code to create and drop database to the DAO.
DavidWhitlock Dec 14, 2025
c8ce0c4
Create an integration test that validates how to persist an H2 databa…
DavidWhitlock Dec 14, 2025
94f11c7
Refactor code that creates the connection to the H2 database to a H2D…
DavidWhitlock Dec 14, 2025
98a6597
Added a Department domain object that is persisted to the database.
DavidWhitlock Dec 14, 2025
209f501
Use the Department class as an example for persisting objects to a fi…
DavidWhitlock Dec 14, 2025
e647012
Make a Course's "departmentId" a foreign key to the "id" field of the…
DavidWhitlock Dec 14, 2025
fdc208a
Automatically generate the "id" for Department objects.
DavidWhitlock Dec 14, 2025
306bcd2
Re-added the one-arg constructor to the Department class.
DavidWhitlock Dec 14, 2025
2657813
Write a simple command line program that persists a Department object…
DavidWhitlock Dec 21, 2025
9b1b8f0
If the name of the department to create isn't provided, then print al…
DavidWhitlock Dec 21, 2025
86690f1
Make the ManageDepartments main method more fully-featured.
DavidWhitlock Dec 21, 2025
98325b5
Run the integration tests as part of the Maven build.
DavidWhitlock Dec 21, 2025
82b6ae4
Added a "credit" property to the Course class.
DavidWhitlock Dec 24, 2025
716b076
Added an "id" property to the Course object that is populated from th…
DavidWhitlock Dec 24, 2025
283b37d
Added an AcademicTerm to show off how dates are persisted to the data…
DavidWhitlock Dec 24, 2025
a897fab
An example of how prepared statements can prevent SQL injection attacks.
DavidWhitlock Dec 28, 2025
8be0347
Use the JDBC Meta Data API to
DavidWhitlock Dec 28, 2025
4056e4e
Introduce DAO interfaces
DavidWhitlock Dec 28, 2025
8e1220b
Inline the createTable() and dropTable() methods from the DAO interfa…
DavidWhitlock Dec 28, 2025
916abff
Data Access Objects (DAOs) for persisting FamilyTree objects to a rel…
DavidWhitlock Dec 30, 2025
ec12cbd
Move the FamilyTree DAO code to the family tree project.
DavidWhitlock Dec 30, 2025
44d9c8e
Move the Family Tree XML example to the resources directory.
DavidWhitlock Dec 30, 2025
9000adb
Added an ExecuteH2DatabaseStatement command line program that execute…
DavidWhitlock Dec 30, 2025
e7295b1
Now that the examples jar no longer depends on the family jar, the we…
DavidWhitlock Dec 30, 2025
dca0b44
With the addition of JDBC content, increment the minor version of the…
DavidWhitlock Jan 1, 2026
eb987c9
Extract the version of H2 into the top-level pom.xml.
DavidWhitlock Jan 1, 2026
ba1a19c
Beginnings of a DAO example for a PhoneBill.
DavidWhitlock Jan 1, 2026
001b574
Extract a skeletal DAO for phone bills.
DavidWhitlock Jan 1, 2026
cd18d82
Only print "interesting" tables in PrintH2DatabaseSchema. That is, i…
DavidWhitlock Jan 1, 2026
2b25be1
Rename the phone bill table names.
DavidWhitlock Jan 1, 2026
84b0ec0
Add a main method to PhoneBillDAO that smoked out a couple of problem…
DavidWhitlock Jan 1, 2026
98f8d75
Increment minor version of phonebill artifacts due to inclusion of JD…
DavidWhitlock Jan 1, 2026
8836530
Remove XML examples from phone bill project. They'll just confuse pe…
DavidWhitlock Jan 1, 2026
e2a2334
Add PhoneBillDAO classes to phone bill archetype project.
DavidWhitlock Jan 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ target
*/target
dependency-reduced-pom.xml

# H2 database files
*.db
26 changes: 20 additions & 6 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>examples</artifactId>
<name>examples</name>
<version>1.3.5-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>
<url>https://www.cs.pdx.edu/~whitlock</url>
<dependencies>
<dependency>
<groupId>io.github.davidwhitlock.joy</groupId>
<artifactId>family</artifactId>
<version>1.1.6-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.github.davidwhitlock.joy</groupId>
<artifactId>projects</artifactId>
Expand All @@ -42,6 +37,11 @@
<version>4.0.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>io.github.davidwhitlock.joy</groupId>
<artifactId>projects</artifactId>
Expand All @@ -50,4 +50,18 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire.version}</version>
</plugin>
</plugins>
</build>
</project>
101 changes: 101 additions & 0 deletions examples/src/it/java/edu/pdx/cs/joy/jdbc/DepartmentDAOIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package edu.pdx.cs.joy.jdbc;

import org.junit.jupiter.api.*;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DepartmentDAOIT {

private static final String TEST_DEPARTMENT_NAME = "Computer Science";
private static int generatedDepartmentId;

private static String dbFilePath;
private Connection connection;
private DepartmentDAO departmentDAO;

@BeforeAll
public static void createTable() throws SQLException {
// Create database file in temporary directory
String tempDir = System.getProperty("java.io.tmpdir");
dbFilePath = tempDir + File.separator + "DepartmentDAOIT.db";

// Connect to the file-based H2 database
Connection connection = H2DatabaseHelper.createFileBasedConnection(new File(dbFilePath));

// Create the departments table
DepartmentDAOImpl.createTable(connection);

connection.close();
}

@BeforeEach
public void setUp() throws SQLException {
// Connect to the existing database file
connection = H2DatabaseHelper.createFileBasedConnection(new File(dbFilePath));
departmentDAO = new DepartmentDAOImpl(connection);
}

@AfterEach
public void tearDown() throws SQLException {
if (connection != null && !connection.isClosed()) {
connection.close();
}
}

@AfterAll
public static void cleanUp() throws SQLException {
// Connect one final time to drop the table and clean up
Connection connection = H2DatabaseHelper.createFileBasedConnection(new File(dbFilePath));
DepartmentDAOImpl.dropTable(connection);
connection.close();

// Delete the database files
deleteIfExists(new File(dbFilePath + ".mv.db"));
deleteIfExists(new File(dbFilePath + ".trace.db"));
}

private static void deleteIfExists(File file) {
if (file.exists()) {
assertThat(file.delete(), is(true));
}
}

@Test
@Order(1)
public void testPersistDepartment() throws SQLException {
// Create and persist a department (ID will be auto-generated)
Department department = new Department(TEST_DEPARTMENT_NAME);
departmentDAO.save(department);

// Store the auto-generated ID for use in subsequent tests
generatedDepartmentId = department.getId();

// Verify that an ID was auto-generated
assertThat(generatedDepartmentId, is(greaterThan(0)));

// Verify the department was saved by fetching it in the same test
Department fetchedDepartment = departmentDAO.findById(generatedDepartmentId);
assertThat(fetchedDepartment, is(notNullValue()));
assertThat(fetchedDepartment.getId(), is(equalTo(generatedDepartmentId)));
assertThat(fetchedDepartment.getName(), is(equalTo(TEST_DEPARTMENT_NAME)));
}

@Test
@Order(2)
public void testFindPersistedDepartment() throws SQLException {
// Search for the department that was persisted in the previous test
// using the auto-generated ID
Department fetchedDepartment = departmentDAO.findById(generatedDepartmentId);

// Validate that the department persisted between test methods
assertThat(fetchedDepartment, is(notNullValue()));
assertThat(fetchedDepartment.getId(), is(equalTo(generatedDepartmentId)));
assertThat(fetchedDepartment.getName(), is(equalTo(TEST_DEPARTMENT_NAME)));
}
}
Loading