SHARE
TWEET

Untitled

a guest Oct 4th, 2017 72 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: mmit352
  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 = "mmit352";
  19.     static final String PASS = "74722d07"
  20.     static final String URL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz:3306/stu_mmit352_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.             // complete this
  28.             Class.forName(JDBC_DRIVER);
  29.                 System.out.println("... Driver Loaded.");
  30.                 conn = DriverManager.getConnection(URL, USER, PASS);
  31.             if (conn != null) {
  32.                 System.out.println("...MySQL Server connected.");
  33.                 populateTable(null0;
  34.             }
  35.             else
  36.                 System.out.println("Failed to make connection!");
  37.         } catch (ClassNotFoundException exp) {
  38.             System.out.println(driver + " Not found");
  39.         } catch (SQLException exp) {
  40.             exp.printStackTrace();
  41.         }
  42.     }
  43.  
  44.     /**
  45.     * Returns the value held at a particular point in the table
  46.     * using the ArrayList<Book>
  47.     * row = the element in the ArrayList
  48.     * col = the particular property of the book
  49.     */
  50.     public Object getValueAt(int row, int col) {
  51.         // complete this
  52.         Book bookReccord = bookList.get(row);
  53.         swtich(col){
  54.             case 0;
  55.                 return bookReccord.getID();
  56.             case 1;
  57.                 return bookReccord.getTitle();
  58.         }
  59.         return bookReccord.getAuthor():
  60.    
  61.     }
  62.        
  63.  
  64.  
  65.  
  66.  
  67.  
  68.         return null;
  69.     }
  70.  
  71.     /**
  72.     * populate the table by executing a sql statement
  73.     */
  74.     public void populateTable(String searchTitle) {
  75.         booklist.clear();
  76.         rows = 0;
  77.         Statement statement = null;
  78.         String sql = null;
  79.         if (searchTitle == null){
  80.             sql = "SELECT book_ID, title, author, ISBN, Publisher, Status from BOOKS";
  81.         }else{
  82.             sql = "SELECT book_ID, title, author, ISBN, Publisher, Status from BOOKS where title LIKE '%" + searchTitle + "%'";
  83.         }
  84.         ResultSet rs = null;
  85.        
  86.         try {
  87.             //complete this
  88.             Class.forName(JDBC_DRIVER);
  89.             conn = DriverManagerConnection(URL, USER, PASS);
  90.             if (conn != null){
  91.                 system.out.println("... Server connected.");
  92.                 statement = conn.createStatement();
  93.                 rs = statement.executeQuery(sql);
  94.                 if (rs != null ){
  95.                     while(rs.next()){
  96.                         Book newBook = new book(rs,getInt(1), rs.getString(2), rs.getString(3));
  97.                         bookList.add(newBook);
  98.                         rows = rows +1;
  99.                     }
  100.                 }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.             fireTableChanged(null); // Tell the listeners a new table has arrived.
  107.         } catch (SQLException exp) {
  108.             System.out.println("Error in : " + sql);
  109.             exp.printStackTrace();
  110.         }
  111.     }
  112.  
  113.     /**
  114.     * return a book's details by executing a sql statement
  115.     */
  116.     public Book getCurrentBook(int row) {
  117.         Statement statement = null;
  118.         String sql = null;
  119.         ResultSet rs = null;
  120.         Book bk = bookList.get(row);
  121.         int bookID = bk.getID();
  122.         try {
  123.             //complete this
  124.             Class.forName(JDBC_DRIVER):
  125.             conn = DriverManager.getConnection(URL, USER, PASS):
  126.             if (conn != null){
  127.                 System.out.println9"...Server connected.");
  128.                 statement = conn.createStatement();
  129.                 sql = 'SELECT book_ID, Title, Author, ISBN, Publisher, Status FROM BOOKS where book_ID Like '%" + "%'";
  130.                 rs = statement .executeQuery(sql);
  131.                 while (rs.next()){
  132.                     bk = new Book (rs.getInt(1), rs.getString (2), rs.getString (3), rs.getString(4), rs.getString(5), rs.getString(6)};
  133.                     bookList.add(bk);
  134.                
  135.                 }
  136.             }
  137.         } catch (SQLException exp) {
  138.             System.out.println("Error in : " + sql);
  139.             exp.printStackTrace();
  140.         } catch(ClassNotFoundException e) {
  141.             e.printStackTrace();
  142.         }
  143.         return bk;
  144.     }
  145.  
  146.     /**
  147.     * Returns number of rows in table
  148.     */
  149.     public int getRowCount() { return rows; }
  150.  
  151.     /**
  152.     * Returns number of columns in table
  153.     */
  154.     public int getColumnCount() { return cols; }
  155.  
  156.     /**
  157.     * Returns the title of a particular column
  158.     */
  159.     public String getColumnName(int col) {return columnName[col];}
  160.  
  161.     /**
  162.     * This indicates to the table object whether the
  163.     * specified cell is editable. by default the key column is
  164.     * not editable. This could be changed to allow user to specify this..
  165.     */
  166.     public boolean isCellEditable(int row, int col) {
  167.         return false;
  168.     }
  169. }
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