summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/bookdatabase/BookDatabase.java
blob: 9f06aba83f27d20db4f85893257f24bbac1c8c6e (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 *      Book Catalog - Catalog your book collection.
 *      
 *      Copyright (C) 2009 Joshua Walters
 *      URL: http://joshwalters.com
 *      
 *      This file is part of Book Catalog.
 *      
 *      Book Catalog is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *      
 *      Book Catalog is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with Book Catalog.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.joshwalters.bookcatalog.bookdatabase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

/**
 * Manages the database of books.
 * 
 * @author Josh Walters
 */
public class BookDatabase {

	/** The SQL statement. */
	private Statement statement;
	/** The SQL result set. */
	private ResultSet resultSet;
	/** The connection to the SQL database. */
	private Connection connection;

	/**
	 * Automatically disconnect from the database.
	 */
	protected void finalize() {
		disconnectFromDatabase();
	}

	/**
	 * Connects to the database. If the table for the record of books does not
	 * exist yet, then it will be created.
	 * 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public void connectToDatabase() throws SQLException, ClassNotFoundException {
		try {
			// Connect to the database.
			Class.forName("org.sqlite.JDBC");
			// Create the database file.
			connection = DriverManager
					.getConnection("jdbc:sqlite:bookcatalog.db");
			statement = connection.createStatement();
		} catch (SQLException e) {
			throw e;
		} catch (ClassNotFoundException e) {
			throw e;
		}
		// Set the timeout to unlimited.
		statement.setQueryTimeout(0);
		// Create the table if it does not exist.
		statement.executeUpdate("create table if not exists bookcatalog("
				+ "Title text, Author text, Date text,"
				+ "Description text, ISBN text, Price text,"
				+ "Publisher text, Subject text, Notes text)");
	}

	/**
	 * Check to see if a book is in the database.
	 * 
	 * @param ISBN
	 * @return True if book is in database, false if not.
	 * @throws SQLException
	 */
	public boolean isBookInDatabase(String ISBN) throws SQLException {
		// The SQL statement.
		PreparedStatement preparedStatement = connection
				.prepareStatement("select * from bookcatalog where ISBN = ?");
		preparedStatement.setString(1, ISBN);
		resultSet = preparedStatement.executeQuery();
		boolean doesBookExist = resultSet.next();
		resultSet.close();
		return doesBookExist;
	}

	/**
	 * Check if a book in the database has a price assigned to it.
	 * 
	 * @param ISBN
	 * @return True if the book has price associated with it. False if not.
	 * @throws SQLException
	 */
	boolean bookHasPrice(String ISBN) throws SQLException {
		// The SQL statement
		PreparedStatement preparedStatement = connection
				.prepareStatement("select Price from bookcatalog where ISBN = ?");
		preparedStatement.setString(1, ISBN);
		resultSet = preparedStatement.executeQuery();
		resultSet.next();
		// Get the price value
		String bookPriceInCatalog = resultSet.getString(1);
		resultSet.close();
		if (bookPriceInCatalog == null) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * Sets the price for a book.
	 * 
	 * @param ISBN
	 * @param Price
	 * @throws SQLException
	 */
	void setBookPrice(String ISBN, String Price) throws SQLException {
		// The SQL statement
		PreparedStatement preparedStatement = connection
				.prepareStatement("update bookcatalog set Price = ? where ISBN = ?");
		preparedStatement.setString(1, Price);
		preparedStatement.setString(2, ISBN);
		if (preparedStatement.execute()) {
			resultSet = statement.getResultSet();
			if (resultSet != null) {
				resultSet.close();
			}
		}
	}

	/**
	 * Delete a book from the database.
	 * 
	 * @param ISBN
	 *            Used to identify the book to delete.
	 * @throws SQLException
	 */
	public void deleteBook(String ISBN) throws SQLException {
		// The SQL statement
		PreparedStatement preparedStatement = connection
				.prepareStatement("delete from bookcatalog where ISBN = ?");
		preparedStatement.setString(1, ISBN);
		if (preparedStatement.execute()) {
			resultSet = statement.getResultSet();
			if (resultSet != null) {
				resultSet.close();
			}
		}
	}

	/**
	 * Updates a book entry with new information.
	 * 
	 * @param Title
	 * @param Author
	 * @param Date
	 * @param Description
	 * @param ISBN
	 * @param Price
	 * @param Publisher
	 * @param Subject
	 * @param Notes
	 * @throws SQLException
	 */
	public void updateBook(String Title, String Author, String Date,
			String Description, String ISBN, String Price, String Publisher,
			String Subject, String Notes) throws SQLException {
		// The SQL statement
		PreparedStatement preparedStatement = connection
				.prepareStatement("update bookcatalog set Title = ?, Author = ?, Date = ?, Description = ?, Price = ?, Publisher = ?, Subject = ?, Notes = ? where ISBN = ?");
		preparedStatement.setString(1, Title);
		preparedStatement.setString(2, Author);
		preparedStatement.setString(3, Date);
		preparedStatement.setString(4, Description);
		preparedStatement.setString(5, Price);
		preparedStatement.setString(6, Publisher);
		preparedStatement.setString(7, Subject);
		preparedStatement.setString(8, Notes);
		preparedStatement.setString(9, ISBN);
		if (preparedStatement.execute()) {
			resultSet = statement.getResultSet();
			if (resultSet != null) {
				resultSet.close();
			}
		}
	}

	/**
	 * Inserts a book into the database.
	 * 
	 * @param Title
	 * @param Author
	 * @param Date
	 * @param Description
	 * @param ISBN
	 * @param Price
	 * @param Publisher
	 * @param Subject
	 * @throws SQLException
	 * @throws BookAlreadyInDatabase
	 */
	public void insertBook(String Title, String Author, String Date,
			String Description, String ISBN, String Price, String Publisher,
			String Subject) throws SQLException, BookAlreadyInDatabase {
		// Check to see if the book is in the database
		if (!isBookInDatabase(ISBN)) {
			// The SQL statement
			PreparedStatement preparedStatement = connection
					.prepareStatement("insert into bookcatalog values(?,?,?,?,?,?,?,?,?);");
			// Set the values for the prepared statement
			preparedStatement.setString(1, Title);
			preparedStatement.setString(2, Author);
			preparedStatement.setString(3, Date);
			preparedStatement.setString(4, Description);
			preparedStatement.setString(5, ISBN);
			preparedStatement.setString(6, Price);
			preparedStatement.setString(7, Publisher);
			preparedStatement.setString(8, Subject);
			// Set the notes for the book blank by default.
			preparedStatement.setString(9, "");
			if (preparedStatement.execute()) {
				resultSet = statement.getResultSet();
				if (resultSet != null) {
					resultSet.close();
				}
			}
		} else {
			// If the book exists in the database check to see if it has a price
			// assigned to it
			if (!bookHasPrice(ISBN) && Price != null) {
				// If the book doesn't have a price associated with it, give it
				// one.
				setBookPrice(ISBN, Price);
			} else {
				// The book is already in the database and we don't have/need
				// the price.
				throw new BookAlreadyInDatabase();
			}
		}
	}

	/**
	 * Get the database in a multidimensional string for use with a Swing table.
	 * 
	 * @return The database in a multidimensional string.
	 * @throws SQLException
	 * @throws EmptyDatabase
	 */
	public String[][] getDatabaseForTable() throws SQLException, EmptyDatabase {
		// Get the number of books in the database.
		resultSet = statement
				.executeQuery("select count(ISBN) from bookcatalog");
		if (resultSet.next()) {
			int rows = resultSet.getInt(1);
			resultSet.close();
			// Select all the data in the database.
			resultSet = statement.executeQuery("select * from bookcatalog");
			// Create a new multidimensional string that will store the
			// database.
			String data[][] = new String[rows][9];
			resultSet.next();
			// Iterate over the database and store the data in the string.
			for (int i = 0; i < rows; i++) {
				for (int j = 1; j <= 9; j++) {
					data[i][j - 1] = resultSet.getString(j);
				}
				resultSet.next();
			}
			resultSet.close();
			// Return the database in string format.
			return data;
		}
		throw new EmptyDatabase();
	}

	/**
	 * This gets the database for a Swing table, but limits the results to
	 * entries that match the search string.
	 * 
	 * @param searchString
	 * @return
	 * @throws SQLException
	 * @throws EmptyDatabase
	 */
	public String[][] searchDatabaseForTable(String searchString)
			throws SQLException, EmptyDatabase {
		String[][] data = null;
		Vector<String> cells = new Vector<String>();
		// Remove all percent signs from the string.
		searchString = searchString.replace("\\%", "");
		// Add percent signs at beginning and end of string.
		searchString = "%" + searchString + "%";
		// Replace space characters with percent signs.
		searchString = searchString.replace(" ", "%");
		/*
		 * Look for entries in the database that are similar to the search
		 * string.
		 */
		PreparedStatement preparedStatement = connection
				.prepareStatement("select * from bookcatalog where Title like ? or Author like ? or Date like ? or Description like ? or ISBN like ? or Price like ? or Publisher like ? or Subject like ? or Notes like ?");
		preparedStatement.setString(1, searchString);
		preparedStatement.setString(2, searchString);
		preparedStatement.setString(3, searchString);
		preparedStatement.setString(4, searchString);
		preparedStatement.setString(5, searchString);
		preparedStatement.setString(6, searchString);
		preparedStatement.setString(7, searchString);
		preparedStatement.setString(8, searchString);
		preparedStatement.setString(9, searchString);
		resultSet = preparedStatement.executeQuery();
		// Store the result data in a vector.
		while (resultSet.next()) {
			for (int i = 1; i <= 9; i++) {
				cells.add(resultSet.getString(i));
			}
		}
		// Create a new multidimensional string to store the database search
		// result.
		data = new String[cells.size() / 9][9];
		// Store the results in the string.
		for (int i = 0; i < cells.size() / 9; i++) {
			for (int j = 0; j < 9; j++) {
				data[i][j] = (String) cells.get((i * 9) + j);
			}
		}
		// Return the data in string format.
		return data;
	}

	/**
	 * Disconnect from the database. This will be called upon GC, but is best to
	 * call it manually.
	 * 
	 * @throws SQLException
	 */
	public void disconnectFromDatabase() {
		try {
			if (connection.isClosed() != true) {
				connection.close();
			}
		} catch (SQLException e) {
			// Do nothing
		}
	}
}