public class DatabaseAccessExample extends Object
Constructor | Description |
---|---|
DatabaseAccessExample() |
Modifier and Type | Method | Description |
---|---|---|
List<Book> |
doTransaction() |
Performs a database transaction which includes clearing the book table, reseting the auto-increment primary key
sequence of the book table, inserting some books, updating the author of a book, and retrieving the resulting
list of books.
|
private List<Book> |
getBooks(Connection connection) |
Retrieves all books from the database.
|
private List<Book> |
getBooksToInsert() |
Generates a list of books to be inserted in the database.
|
private void |
insertBooks(Connection connection,
List<Book> books) |
Inserts the specified books into the database.
|
static void |
main(String[] args) |
Runs the example.
|
private void |
printBooks(String prefix,
List<Book> books) |
Prints a list of books by calling
Book.toString() on each book and pre-pending the resulting string with
the specified prefix. |
private void |
removeBooks(Connection connection) |
Removes all books from the database and resets the auto-increment primary key sequence.
|
private void |
updateBook(Connection connection,
Book book) |
Updates the book with the ID of the specified book, setting all values to the current values of the specified book.
|
public static void main(String[] args)
args
- not used.public List<Book> doTransaction() throws SQLException
SQLException
- if an SQL error occurs.private List<Book> getBooksToInsert()
private void printBooks(String prefix, List<Book> books)
Book.toString()
on each book and pre-pending the resulting string with
the specified prefix. The prefix is useful for indenting the printout of books.prefix
- the prefix inserted before each book in the printout.books
- the books to be printed.private void removeBooks(Connection connection) throws SQLException
connection
- the connection to be used for this database operation.SQLException
- if an SQL error occurs.private void insertBooks(Connection connection, List<Book> books) throws SQLException
connection
- the connection to be used for this database operation.books
- the books to be inserted.SQLException
- if an SQL error occurs.private List<Book> getBooks(Connection connection) throws SQLException
connection
- the connection to be used for the database operation.SQLException
- if an SQL error occurs.private void updateBook(Connection connection, Book book) throws SQLException
connection
- the connection to be used for the database operation.book
- the book to be updated.SQLException
- if an SQL error occurs.