Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ============================================================================================
- * BookTableModel.java : A table model
- * YOUR UPI: mmit352
- * ============================================================================================
- */
- import java.sql.*;
- import java.util.*;
- import javax.swing.table.*;
- public class BookTableModel extends AbstractTableModel {
- private ArrayList<Book> bookList;
- private int rows, cols = 3;
- private String[] columnName;
- private Connection conn = null;
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String USER = "mmit352";
- static final String PASS = "74722d07"
- static final String URL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz:3306/stu_mmit352_COMPSCI_280_C_S2_2017";
- public BookTableModel(String driver, String url, String username, String password) {
- columnName = new String[]{"ID", "Title", "Author"};
- System.out.println("Connecting...");
- bookList = new ArrayList<Book>();
- try {
- // complete this
- Class.forName(JDBC_DRIVER);
- System.out.println("... Driver Loaded.");
- conn = DriverManager.getConnection(URL, USER, PASS);
- if (conn != null) {
- System.out.println("...MySQL Server connected.");
- populateTable(null0;
- }
- else
- System.out.println("Failed to make connection!");
- } catch (ClassNotFoundException exp) {
- System.out.println(driver + " Not found");
- } catch (SQLException exp) {
- exp.printStackTrace();
- }
- }
- /**
- * Returns the value held at a particular point in the table
- * using the ArrayList<Book>
- * row = the element in the ArrayList
- * col = the particular property of the book
- */
- public Object getValueAt(int row, int col) {
- // complete this
- Book bookReccord = bookList.get(row);
- swtich(col){
- case 0;
- return bookReccord.getID();
- case 1;
- return bookReccord.getTitle();
- }
- return bookReccord.getAuthor():
- }
- return null;
- }
- /**
- * populate the table by executing a sql statement
- */
- public void populateTable(String searchTitle) {
- booklist.clear();
- rows = 0;
- Statement statement = null;
- String sql = null;
- if (searchTitle == null){
- sql = "SELECT book_ID, title, author, ISBN, Publisher, Status from BOOKS";
- }else{
- sql = "SELECT book_ID, title, author, ISBN, Publisher, Status from BOOKS where title LIKE '%" + searchTitle + "%'";
- }
- ResultSet rs = null;
- try {
- //complete this
- Class.forName(JDBC_DRIVER);
- conn = DriverManagerConnection(URL, USER, PASS);
- if (conn != null){
- system.out.println("... Server connected.");
- statement = conn.createStatement();
- rs = statement.executeQuery(sql);
- if (rs != null ){
- while(rs.next()){
- Book newBook = new book(rs,getInt(1), rs.getString(2), rs.getString(3));
- bookList.add(newBook);
- rows = rows +1;
- }
- }
- fireTableChanged(null); // Tell the listeners a new table has arrived.
- } catch (SQLException exp) {
- System.out.println("Error in : " + sql);
- exp.printStackTrace();
- }
- }
- /**
- * return a book's details by executing a sql statement
- */
- public Book getCurrentBook(int row) {
- Statement statement = null;
- String sql = null;
- ResultSet rs = null;
- Book bk = bookList.get(row);
- int bookID = bk.getID();
- try {
- //complete this
- Class.forName(JDBC_DRIVER):
- conn = DriverManager.getConnection(URL, USER, PASS):
- if (conn != null){
- System.out.println9"...Server connected.");
- statement = conn.createStatement();
- sql = 'SELECT book_ID, Title, Author, ISBN, Publisher, Status FROM BOOKS where book_ID Like '%" + "%'";
- rs = statement .executeQuery(sql);
- while (rs.next()){
- bk = new Book (rs.getInt(1), rs.getString (2), rs.getString (3), rs.getString(4), rs.getString(5), rs.getString(6)};
- bookList.add(bk);
- }
- }
- } catch (SQLException exp) {
- System.out.println("Error in : " + sql);
- exp.printStackTrace();
- } catch(ClassNotFoundException e) {
- e.printStackTrace();
- }
- return bk;
- }
- /**
- * Returns number of rows in table
- */
- public int getRowCount() { return rows; }
- /**
- * Returns number of columns in table
- */
- public int getColumnCount() { return cols; }
- /**
- * Returns the title of a particular column
- */
- public String getColumnName(int col) {return columnName[col];}
- /**
- * This indicates to the table object whether the
- * specified cell is editable. by default the key column is
- * not editable. This could be changed to allow user to specify this..
- */
- public boolean isCellEditable(int row, int col) {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement