SHARE
TWEET

Untitled

a guest Oct 4th, 2017 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3.  *  ============================================================================================
  4.  *  BookTableModel.java : A table model
  5.  *  YOUR UPI: hkau513
  6.  *  ============================================================================================
  7.  */
  8. import java.sql.*;
  9. import java.util.*;
  10. import javax.swing.table.*;
  11.  
  12. public class BookTableModel extends AbstractTableModel {
  13.     private ArrayList<Book> bookList;
  14.     private int rows, cols = 3;
  15.     private String[] columnName;
  16.     private Connection conn = null;
  17.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  18.     static final String USER = "hkau515";
  19.     static final String PASS = "6df92758";
  20.     static final String URL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz:3306/stu_hkau513_COMPSCI_280_C_S2_2017";
  21.  
  22.     public BookTableModel(String driver, String url, String username, String password)  {
  23.         columnName = new String[]{"ID", "Title", "Author"};
  24.         System.out.println("Connecting...");
  25.         bookList = new ArrayList<Book>();
  26.         try {
  27.             Class.forName(JDBC_DRIVER);
  28.             System.out.println("... Driver Loaded.");
  29.             connect = DriverManager.getConnection(URL, USER, PASS);
  30.  
  31.             if (connect != null){
  32.                 System.out.println("...MySQL Server connected.");
  33.                 populateTable(null);
  34.             }
  35.            
  36.             else
  37.                 System.out.println("Failed to make connection!");
  38.         } catch (ClassNotFoundException exp) {
  39.             System.out.println(driver + " Not found");
  40.         } catch (SQLException exp) {
  41.             exp.printStackTrace();
  42.         }
  43.     }
  44.  
  45.     /**
  46.     * Returns the value held at a particular point in the table
  47.     * using the ArrayList<Book>
  48.     * row = the element in the ArrayList
  49.     * col = the particular property of the book
  50.     */
  51.     public Object getValueAt(int row, int col) {
  52.         Book bookRecord = bookList.get(row);
  53.         switch(col){
  54.             case 0:
  55.                 return bookRecord.getID();
  56.             case 1:
  57.                 return bookRecord.getTitle();
  58.         }
  59.         return bookRecord.getAuthor();
  60.     }
  61.  
  62.     /**
  63.     * populate the table by executing a sql statement
  64.     */
  65.     public void populateTable(String searchTitle) {
  66.         bookList.clear();
  67.         rows = 0;
  68.         Statement statement = null;
  69.         String sql = null;
  70.         if (searchTitle == null){
  71.             sql = "SELECT book_ID, title, author,ISBN,Publisher,Status FROM BOOKS";
  72.         } else{
  73.             sql = "SELECT book_ID, title, author,ISBN,Publisher,Status FROM BOOKS WHERE title LIKE '%" + searchTitle + "%'";
  74.         }
  75.         ResultSet result = null;
  76.         try {
  77.             //complete this
  78.             Class.forName(JDBC_DRIVER);
  79.             connect = DriverManagerConnection(URL, USER, PASS);
  80.             if (connect != null){
  81.                 system.out.println("... MySQL Server connected.");
  82.                 statement = connect.createStatement();
  83.                 result = statement.executeQuery(sql);
  84.                 if (result != null ){
  85.                     while(result.next()){
  86.                         book = new Book(result.getInt(1), result.getString(2), result.getString(3));
  87.                         bookList.add(newBook);
  88.                         rows = rows +1;
  89.                     }
  90.                 }
  91.             }
  92.            
  93.         fireTableChanged(null); // Tell the listeners a new table has arrived.
  94.         } catch (ClassNotFoundException exp) {
  95.             System.out.println(driver + " Not found");
  96.         } catch (SQLException exp) {
  97.             exp.printStackTrace();
  98.         }
  99.     }
  100.  
  101.     /**
  102.     * return a book's details by executing a sql statement
  103.     */
  104.     public Book getCurrentBook(int row) {
  105.         Statement statement = null;
  106.         String sql = null;
  107.         ResultSet result = null;
  108.         Book book = bookList.get(row);
  109.         int bookID = book.getID();
  110.        
  111.         try {
  112.             Class.forName(JDBC_DRIVER);
  113.             connect = DriverManagerConnection(URL, USER, PASS);
  114.             if (connect != null){
  115.                 system.out.println("... MySQL Server connected.");
  116.                 statement = connect.createStatement();
  117.                 sql = "SELECT book_ID, Title, Author, ISBN, Publisher, Status FROM BOOKS WHERE book_ID Like '%" + "%'";
  118.                 result = statement.executeQuery(sql);
  119.                 if (result != null ){
  120.                     while(result.next()){
  121.                         book = new Book(result.getInt(1), result.getString(2), result.getString(3), result.getString(4),result.getString(5),result.getString(6));
  122.                         bookList.add(book);
  123.                     }
  124.                 }
  125.             }
  126.         } catch (SQLException exp) {
  127.             System.out.println("Error in : " + sql);
  128.             exp.printStackTrace();
  129.         } catch (ClassNotFoundException exp){
  130.             exp.printStackTrace();
  131.         }
  132.            
  133.        
  134.        
  135.         return book;
  136.     }
  137.  
  138.     /**
  139.     * Returns number of rows in table
  140.     */
  141.     public int getRowCount() { return rows; }
  142.  
  143.     /**
  144.     * Returns number of columns in table
  145.     */
  146.     public int getColumnCount() { return cols; }
  147.  
  148.     /**
  149.     * Returns the title of a particular column
  150.     */
  151.     public String getColumnName(int col) {return columnName[col];}
  152.  
  153.     /**
  154.     * This indicates to the table object whether the
  155.     * specified cell is editable. by default the key column is
  156.     * not editable. This could be changed to allow user to specify this..
  157.     */
  158.     public boolean isCellEditable(int row, int col) {
  159.         return false;
  160.     }
  161. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top