Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
CRUD operations | Basic Data Structures
Java Data Structures

CRUD operationsCRUD operations

Basic Operations for a Database

In the previous chapter, you completed a fairly complex and interesting task on library management. You implemented methods for adding, retrieving, and removing elements from the list there. Did you know that such operations are essential for every list and application? These operations are referred to as CRUD operations!

CRUD stands for CREATE, READ, UPDATE, and DELETE, representing a list of operations to be performed on a database. In our case, in the previous task, the class Library served as our database, specifically the list of books. We wrote fundamental operations for working with this list, slightly customizing each of them. We created more focused methods for retrieving a book by author or obtaining a list of books published after a specific year.

Similar operations are employed in large applications to interact with databases. In the end, our application should:

  1. Be able to create data in the database;
  2. Be able to read data from the database;
  3. Be able to update specific data in the database;
  4. Be able to delete data from the database.

Note

Note that in our case, the database is represented by a data structure, a list, which currently serves as our workaround. In the future, instead of a simple list, we will use a database.

You may have noticed we didn't implement the update method in the previous task.

Let's do that now:

We first need to retrieve the book by ID to update an element by ID. For this, we will implement a method that fetches the book by ID. It's better to do it this way than to implement the logic of getting a book by its ID in the book update method, as we can reuse this method in other functions, making it more versatile.

java

Library.java

The solution above is a good option, but there is a small problem. We will get an error if such an ID does not exist in our list. We need to handle this error and display a message on the screen indicating that there is no such book in the list (database).

java

Library.java

We initially initialize the variable result as null in this solution. After implementing the book search by ID, we perform a check to see if our result is still null. If it's true, we create a new undefined book. Let's move on to implementing the update method:

java

Library.java

Great! We updated the book by updating all its parameters. I highly recommend doing it this way because if we simply reassign the book, unexpected errors may occur. This way, we update the book by ID, reassigning it to the parameters of the new book. Now, let's use the update method and see what happens:

java

main.java

Now, the Library class has all CRUD operations.

You've done a great job if you grasped all of this. I recommend copying the methods I showed in this chapter and adding them to your code from the previous assignment, as this updated code will come in handy later.

1. What does the acronym **CRUD** mean?
2. Why is it necessary to implement CRUD operations in your code?
3. Can CRUD operations be used with databases?

What does the acronym CRUD mean?

Виберіть правильну відповідь

Why is it necessary to implement CRUD operations in your code?

Виберіть правильну відповідь

Can CRUD operations be used with databases?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 4
course content

Зміст курсу

Java Data Structures

CRUD operationsCRUD operations

Basic Operations for a Database

In the previous chapter, you completed a fairly complex and interesting task on library management. You implemented methods for adding, retrieving, and removing elements from the list there. Did you know that such operations are essential for every list and application? These operations are referred to as CRUD operations!

CRUD stands for CREATE, READ, UPDATE, and DELETE, representing a list of operations to be performed on a database. In our case, in the previous task, the class Library served as our database, specifically the list of books. We wrote fundamental operations for working with this list, slightly customizing each of them. We created more focused methods for retrieving a book by author or obtaining a list of books published after a specific year.

Similar operations are employed in large applications to interact with databases. In the end, our application should:

  1. Be able to create data in the database;
  2. Be able to read data from the database;
  3. Be able to update specific data in the database;
  4. Be able to delete data from the database.

Note

Note that in our case, the database is represented by a data structure, a list, which currently serves as our workaround. In the future, instead of a simple list, we will use a database.

You may have noticed we didn't implement the update method in the previous task.

Let's do that now:

We first need to retrieve the book by ID to update an element by ID. For this, we will implement a method that fetches the book by ID. It's better to do it this way than to implement the logic of getting a book by its ID in the book update method, as we can reuse this method in other functions, making it more versatile.

java

Library.java

The solution above is a good option, but there is a small problem. We will get an error if such an ID does not exist in our list. We need to handle this error and display a message on the screen indicating that there is no such book in the list (database).

java

Library.java

We initially initialize the variable result as null in this solution. After implementing the book search by ID, we perform a check to see if our result is still null. If it's true, we create a new undefined book. Let's move on to implementing the update method:

java

Library.java

Great! We updated the book by updating all its parameters. I highly recommend doing it this way because if we simply reassign the book, unexpected errors may occur. This way, we update the book by ID, reassigning it to the parameters of the new book. Now, let's use the update method and see what happens:

java

main.java

Now, the Library class has all CRUD operations.

You've done a great job if you grasped all of this. I recommend copying the methods I showed in this chapter and adding them to your code from the previous assignment, as this updated code will come in handy later.

1. What does the acronym **CRUD** mean?
2. Why is it necessary to implement CRUD operations in your code?
3. Can CRUD operations be used with databases?

What does the acronym CRUD mean?

Виберіть правильну відповідь

Why is it necessary to implement CRUD operations in your code?

Виберіть правильну відповідь

Can CRUD operations be used with databases?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 4
some-alt