Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution to review #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

added solution to review #1

wants to merge 1 commit into from

Conversation

JJJazl
Copy link
Owner

@JJJazl JJJazl commented Sep 18, 2024

No description provided.

@@ -0,0 +1,9 @@
CREATE DATABASE `taxi_db`;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a ' ' for database/table/column name is optional

Comment on lines +4 to +6
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Remember about SQL style: use uppercase for SQL keywords in your queries
  2. You don't have to specify NULL as a default value explicitly

Comment on lines +4 to +7
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`is_deleted` tinyint NOT NULL DEFAULT '0',
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s use TRUE/FALSE instead of 1/0 with is_deleted column

`name` varchar(255) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`is_deleted` tinyint NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to give an explicit name for constrains like: PRIMARY/FOREIGN KEYS and UNIQUE columns so that it is easier to identify the column causing the error if one occurs

try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use informative messages for exceptions Even like this.
For example: "Can't initialize MySQL driver"

}

@Override
public void delete(Long id) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task, method should return boolean
Don’t return true all the time. Return boolean value depending on executeUpdate() :
deletedRows > 0

Comment on lines +66 to +67
"SELECT manufacturers.id, manufacturers.name, manufacturers.country"
+ " FROM manufacturers WHERE is_deleted = 0");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use column names without table name
  2. Let’s use TRUE/FALSE instead of 1/0 with is_deleted column in the sql queries. The same in method delete()

Comment on lines +105 to +106
updateStatement.executeUpdate();
return new Manufacturer();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check if update was successful: updatedRows > 0 and return the passed manufacturer (not a new one) if so or throw an DataProcessingException exception otherwise

Comment on lines +11 to +14
Manufacturer manufacturer1 = new Manufacturer(null, "Lincoln", "USA");
Manufacturer manufacturer2 = new Manufacturer(null, "Ford", "USA");
Manufacturer manufacturer3 = new Manufacturer(null, "Audi", "Germany");
Manufacturer manufacturer4 = new Manufacturer(null, "BMW", "Germany");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use an informative variable names
  2. Use a constructor with 2 parameters: name, country

Comment on lines +15 to +27
manufacturerDao.create(manufacturer1);
manufacturerDao.create(manufacturer2);
manufacturerDao.create(manufacturer3);
manufacturerDao.create(manufacturer4);

manufacturerDao.delete(16L);
manufacturerDao.delete(22L);
manufacturerDao.delete(24L);
manufacturerDao.delete(27L);

System.out.println(manufacturerDao.get(23L));
manufacturerDao.update(new Manufacturer(23L, "Volkswagen", "Germany"));
System.out.println(manufacturerDao.get(23L));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Override method toString() in the Manufacturer class
  2. Check if records were inserted by calling methods get() and getAll()
  3. Check if record(s) were deleted by calling methods delete() and get()

Comment on lines +47 to +50
Long idOfManufacturer = resultSet.getLong("id");
String name = resultSet.getString("name");
String country = resultSet.getString("country");
manufacturer = new Manufacturer(idOfManufacturer, name, country);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to avoid code duplication. Move retrieving data from ResultSet into Entity object to a separate private method and reuse it in other methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants