-
-
Save Mahkul/911437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pawel.bookshelf; | |
import java.sql.*; | |
public class BookShelf { | |
private String user = ""; | |
private String password = ""; | |
static String url = "jdbc:odbc:Books"; | |
static Connection link; | |
static Statement statement; | |
static ResultSet results; | |
public BookShelf() { | |
getConnection(); //Create Connection | |
} | |
public Connection getConnection(){ | |
try { | |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); | |
} catch(java.lang.ClassNotFoundException e) { | |
System.err.print("ClassNotFoundException: "); | |
System.err.println(e.getMessage()); | |
} | |
try { | |
link = DriverManager.getConnection(url, user, password); | |
statement = link.createStatement(); | |
} catch(SQLException ex) { | |
System.err.println("SQLException: " + ex.getMessage()); | |
} | |
return link; | |
} | |
public int getMaxID() throws SQLException | |
{ | |
try | |
{ | |
String select = "SELECT MAX(ID) FROM BookShelf"; | |
// Step 4 | |
results = statement.executeQuery(select); | |
results.next(); | |
return results.getInt(1); | |
} | |
catch(Exception e){ | |
System.out.println(e); | |
return 0; | |
} | |
} | |
public int highestPricePaid() | |
{ | |
try | |
{ | |
String select = "SELECT MAX(Cost) FROM BookShelf"; | |
// Step 4 | |
results = statement.executeQuery(select); | |
results.next(); | |
return results.getInt(1); | |
} | |
catch(Exception e){ | |
System.out.println(e); | |
return 0; | |
} | |
} | |
public int sizeOfBookshelf() | |
{ | |
try | |
{ | |
String select = "Select COUNT(ID) FROM BookShelf"; | |
// Step 4 | |
results = statement.executeQuery(select); | |
results.next(); | |
return results.getInt(1); | |
} | |
catch(Exception e){ | |
System.out.println(e); | |
return 0; | |
} | |
} | |
public int costOfBookshelf() | |
{ | |
try | |
{ | |
String select = "Select SUM(Cost) FROM BookShelf"; | |
results = statement.executeQuery(select); | |
results.next(); | |
return results.getInt(1); | |
} | |
catch(Exception e){ | |
System.out.println(e); | |
return 0; | |
} | |
} | |
public void addBook(Book newBook){ | |
try | |
{ | |
int new_id = getMaxID() +1; | |
String query = "INSERT INTO BookShelf(ID, Title, Author, Cost, Publisher, YearOfPublishing) VALUES (" | |
+ new_id | |
+ ",'" + newBook.getTitle() | |
+ "','" + newBook.getAuthor() | |
+ "'," + newBook.getCost() | |
+ ",'" + newBook.getPublisher() + "'," | |
+ newBook.getYear() + ")"; | |
int result = statement.executeUpdate(query); | |
if (result == 0){ | |
System.out.println("Failed to insert the following data: " | |
+ newBook.getTitle() + "," | |
+ newBook.getAuthor() + "," | |
+ newBook.getCost() + "," | |
+ newBook.getPublisher() + "," | |
+ newBook.getYear()); | |
} | |
} | |
catch(Exception e){ | |
System.out.println(e); | |
} | |
} | |
} / |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment