generated from ohjelmointi2/gradle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlbumDAO.java
More file actions
108 lines (99 loc) · 4.05 KB
/
AlbumDAO.java
File metadata and controls
108 lines (99 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package databases.part03;
import java.util.List;
import databases.part02.Artist;
/**
* Data Access Object for the Album table in the Chinook database.
*/
public class AlbumDAO {
/**
* The connection string used to connect to the database. You MUST use this
* string when connecting to the database using JDBC. In the unit tests, this
* field will be set to a different value.
*/
private final String connectionString;
/**
* Creates a new AlbumDAO that uses the specified connection string to connect
* to the database. For example: "jdbc:sqlite:data/Chinook_Sqlite.sqlite"
*
* @param jdbcConnection see https://www.baeldung.com/java-jdbc-url-format
*/
public AlbumDAO(String jdbcConnection) {
this.connectionString = jdbcConnection;
}
/**
* Returns a list of all albums that have the specified artist as the artist.
* If there are no albums for the specified artist, the list is empty.
*
* @param artist the artist whose albums to retrieve.
* @return a list of all albums that have the specified artist as the artist,
* sorted by AlbumId in ascending order.
*/
public List<Album> getAlbumsByArtist(Artist artist) {
/*
* hint: use the artist.getId() method to get the artist's id and add it to the
* SQL query using PreparedStatement's setLong() method. See the previous hint
* in ArtistDao.getArtist() for an example.
*
* Note that you must use the `connectionString` field in this class to connect
* to the database. You can't "hard code" the connection string, as that would
* make tests run against your actual database, which may have unexpected data.
*
* Make sure to sort the albums by AlbumId in ascending order (low to high).
*/
return null;
}
/**
* Adds the specified album to the database. Returns true if the album was
* added successfully, false otherwise.
*
* @param album the album to add to the database.
* @return true if the album was added successfully, false otherwise.
*/
public boolean addAlbum(Album album) {
/*
* hint 1: use PreparedStatement's setString() and setLong() methods to
* add the album's title and artist id to the SQL query. Leave the AlbumId
* blank, as it will be automatically generated by the database.
*
* hint 2: executeUpdate() returns the number of rows affected by the query.
* If the number of rows affected is greater than 0, the album was added.
*
* Remember to use the `connectionString` instead of hard coding it ;)
*/
return false;
}
/**
* Updates the specified album in the database. Returns true if the album was
* updated successfully, false otherwise.
*
* @param album the album to update in the database.
* @return true if the album was updated successfully, false otherwise.
*/
public boolean updateAlbum(Album album) {
/*
* hint 1: use PreparedStatement's setString() and setLong() methods to
* add the album's title and artist id to the SQL query. Do not change the
* AlbumId, but use it to identify the album to update.
*
* Remember to use the `connectionString` instead of hard coding it, otherwise
* your tests will be deleting albums from your actual database!
*/
return false;
}
/**
* Deletes the specified album from the database. Returns true if the album was
* deleted successfully, false otherwise.
*
* @param album the album to delete from the database.
* @return true if the album was deleted successfully, false otherwise.
*/
public boolean deleteAlbum(Album album) {
/*
* See hints for the methods above.
*
* Remember to use the `connectionString` instead of hard coding it, otherwise
* your tetss will be deleting albums from your actual database!
*/
return false;
}
}