View difference between Paste ID: NKveUxT8 and g3uDQ29E
SHOW: | | - or go back to the newest paste.
1
public class Book {
2
3
    final String name;
4
    final String isbn;
5
    final String authorName;
6
7
    public Book(String name, String isbn, String authorName) {
8
        this.name = name;
9
        this.isbn = isbn;
10
        this.authorName = authorName;
11
    }
12
13
    @Override
14
    public String toString() {
15
        return String.format("%s by %s (ISBN: %s)", name, authorName, isbn);
16
    }
17
}
18
19
20
/**
21
 * An abstraction for the database operations
22
 * Provides a simpler interface for interacting with data
23
 */
24
public class BooksRepository {
25
26
    // You will need something like below to connect to an Oracle database
27
    // private static final String CONNECTION_URL = "jdbc:oracle:thin:localhost:1521/";
28
    // private static final String DB_USER = "abhijeet";
29
    //private static final String DB_PASSWORD = "something";
30
31
    // Here I am using an SQLite database for demonstration
32
    // shouldn't make much difference except for query syntax and data types.
33
    private static final String CONNECTION_URL = "jdbc:sqlite:books.db";
34
35
    private final Connection connection;
36
37
    public BooksRepository() throws SQLException {
38
        this.connection = DriverManager.getConnection(CONNECTION_URL);
39
    }
40
41
    public void insertBook(Book book) throws SQLException {
42
        final PreparedStatement statement =
43
                connection.prepareStatement("INSERT INTO Books(name, authorName, isbn) values(?, ?, ?)");
44
45
        statement.setString(1, book.name);
46
        statement.setString(2, book.authorName);
47
        statement.setString(3, book.isbn);
48
49
        statement.executeUpdate();
50
    }
51
52
    /**
53
     * Fetches all books from the database
54
     */
55
    public List<Book> getAllBooks() throws SQLException {
56
        final Statement statement = connection.createStatement();
57
        final ResultSet result = statement.executeQuery("SELECT * FROM Books");
58
59
        final ArrayList<Book> books = new ArrayList<Book>();
60
61
        while (result.next()) {
62
            final String name = result.getString("name");
63
            final String isbn = result.getString("isbn");
64
            final String authorName = result.getString("authorName");
65
66
            books.add(new Book(name, isbn, authorName));
67
        }
68
69
        return books;
70
    }
71
72
    /**
73
     * Fetches a book with specific ISBN
74
     * if no such book exists, null is returned
75
     */
76
    public Book getBookByIsbn(String matchIsbn) throws SQLException {
77
        final PreparedStatement statement = connection.prepareStatement("SELECT * FROM BOOKS WHERE isbn = ?");
78
79
        statement.setString(1, matchIsbn);
80
        final ResultSet result = statement.executeQuery();
81
82
        if (result.next()) {
83
            final String name = result.getString("name");
84
            final String isbn = result.getString("isbn");
85
            final String authorName = result.getString("authorName");
86
87
            return new Book(name, isbn, authorName);
88
        } else {
89
            return null;
90
        }
91
    }
92
93
    /**
94
     * Fetches all the books with the name that match a given string
95
     */
96
    public List<Book> getBooksByName(String matchName) throws SQLException {
97
        final PreparedStatement statement = connection.prepareStatement("SELECT * FROM Books WHERE name LIKE ?");
98
99
        statement.setString(1, '%' + matchName + '%');
100
        final ResultSet result = statement.executeQuery();
101
102
        final ArrayList<Book> books = new ArrayList<Book>();
103
104
        while (result.next()) {
105
            final String name = result.getString("name");
106
            final String isbn = result.getString("isbn");
107
            final String authorName = result.getString("authorName");
108
109
            books.add(new Book(name, isbn, authorName));
110
        }
111
112
        return books;
113
    }
114
115
    public void close() throws SQLException {
116
        if (!this.connection.isClosed()) {
117
            this.connection.close();
118
        }
119
    }
120
}
121
122
123
public class Main {
124
125
    private static Book inputBook() {
126
        System.out.println("Enter book name, author and isbn on separate lines:");
127
        String name = Stdin.readLine();
128
        String authorName = Stdin.readLine();
129
        String isbn = Stdin.readLine();
130
131
        return new Book(name, authorName, isbn);
132
    }
133
134
    public static void main(String[] args) throws SQLException {
135
136
        final BooksRepository repo = new BooksRepository();
137
138
        // Add a new book to database
139
        final Book newBook = inputBook();
140
        repo.insertBook(newBook);
141
142
        // List out all books
143
        System.out.println("All books:");
144
145
        final List<Book> allBooks = repo.getAllBooks();
146
        for (final Book book : allBooks) {
147
            System.out.println(book);
148
        }
149
150
        // Search for books
151
        System.out.println("Books with search term:");
152
153
        final List<Book> booksWithSInName = repo.getBooksByName("s");
154
        for (final Book book : booksWithSInName) {
155
            System.out.println(book);
156
        }
157
158
        // Look up a specific book
159
        System.out.println("Book with specific ISBN:");
160
161
        final Book myBook = repo.getBookByIsbn("123");
162
        System.out.println(myBook);
163
164
        // Close out repository after we are done with it
165
        repo.close();
166
    }
167
}