Skip to content

Commit

Permalink
fixed checkstyle for App Class, created package-info
Browse files Browse the repository at this point in the history
  • Loading branch information
ipaulaandreea committed Nov 5, 2024
1 parent f6908f0 commit bc33aba
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 11 deletions.
75 changes: 66 additions & 9 deletions active-record/src/main/java/com/iluwatar/activerecord/App.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
package com.iluwatar.activerecord;
/*
* This project is licensed under the MIT license.
* Module model-view-viewmodel is using ZK framework
* licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.activerecord;
import lombok.extern.slf4j.Slf4j;
import java.sql.SQLException;
import java.util.List;

public class App {
/**
* The Active Record pattern is an architectural pattern that simplifies
* database interactions by encapsulating database access logic within
* the domain model. This pattern allows objects to be responsible for
* their own persistence, providing methods for CRUD operations directly
* within the model class.
*
* <p>In this example, we demonstrate the Active Record pattern
* by creating, updating, retrieving, and deleting user records in
* a SQLite database. The User class contains methods to perform
* these operations, ensuring that the database interactions are
* straightforward and intuitive.
*/

@Slf4j
public final class App {

private App() {
throw new UnsupportedOperationException("Utility class");
}

/**
* Entry Point.
*
* @param args the command line arguments - not used
*/

public static void main(String[] args) {

public static void main(final String[] args) {
try {
// Initialize the database and create the users table if it doesn't exist
User.initializeTable();
Expand All @@ -14,12 +67,14 @@ public static void main(String[] args) {
// Create a new user and save it to the database
User user1 = new User(null, "John Doe", "[email protected]");
user1.save();
System.out.println("New user saved: " + user1.getName() + " with ID " + user1.getId());
System.out.println("New user saved: " + user1.getName()
+ " with ID " + user1.getId());

// Retrieve and display the user by ID
User foundUser = User.findById(user1.getId());
if (foundUser != null) {
System.out.println("User found: " + foundUser.getName() + " with email " + foundUser.getEmail());
System.out.println("User found: " + foundUser.getName()
+ " with email " + foundUser.getEmail());
} else {
System.out.println("User not found.");
}
Expand All @@ -29,22 +84,24 @@ public static void main(String[] args) {
foundUser.setName("John Updated");
foundUser.setEmail("[email protected]");
foundUser.save();
System.out.println("User updated: " + foundUser.getName() + " with email " + foundUser.getEmail());
System.out.println("User updated: " + foundUser.getName()
+ " with email " + foundUser.getEmail());

// Retrieve all users
List<User> users = User.findAll();
System.out.println("All users in the database:");
for (User user : users) {
System.out.println("ID: " + user.getId() + ", Name: " + user.getName() + ", Email: " + user.getEmail());
System.out.println("ID: " + user.getId()
+ ", Name: " + user.getName()
+ ", Email: " + user.getEmail());
}

// Delete the user
try {
System.out.println("Deleting user with ID: " + foundUser.getId());
foundUser.delete();
System.out.println("User successfully deleted!");
}
catch (Exception e){
} catch (Exception e) {
System.out.println(e.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.iluwatar.activerecord;

import lombok.Data;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This project is licensed under the MIT license.
* Module model-view-viewmodel is using
* ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/**
* Context and problem
* Most applications require a way to interact with a database to perform
* CRUD (Create, Read, Update, Delete) operations.
* Traditionally, this involves writing a lot of boilerplate code
* for database access, which can be error-prone and hard to maintain.
* This is especially true in applications with
* complex data models and relationships.
*
* <p>Often, developers need to write separate classes for database access and
* domain models, leading to a separation of concerns
* that can make the codebase more complex and harder to understand.
* Additionally, changes to the database schema can require significant
* changes to the data access code, increasing the maintenance burden.
*
* <p>Maintaining this separation can force the application to
* adhere to at least some of the database's APIs or other semantics.
* When these database features have quality issues, supporting them "corrupts"
* what might otherwise be a cleanly designed application.
* Similar issues can arise with any external system that your
* development team doesn't control, not just databases.
*
* <p>Solution Simplify database interactions by using
* the Active Record pattern.
* This pattern encapsulates database access logic within the
* domain model itself, allowing objects to be responsible
* for their own persistence.
* The Active Record pattern provides methods for
* CRUD operations directly within the model class,
* making database interactions straightforward and intuitive.
*/

package com.iluwatar.activerecord;
Binary file removed database.db
Binary file not shown.
Binary file removed my_database.db
Binary file not shown.

0 comments on commit bc33aba

Please sign in to comment.