SHARE
TWEET

Untitled

a guest Jun 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.javaoo.store.dao;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5. import com.javaoo.store.*;
  6.  
  7. public class InventoryDAO {
  8.     private Connection connection;
  9.     private Statement  statement;
  10.    
  11.     private final String driverClass      = "org.apache.derby.jdbc.EmbeddedDriver";
  12.     private final String databaseURL      = "jdbc:derby:c:\\sample;create=true";
  13.     private final String databaseUser     = "";
  14.     private final String databasePassword = "";
  15.    
  16.     public InventoryDAO() {
  17.         try {
  18.             Class.forName(driverClass);
  19.             connection = DriverManager.getConnection( databaseURL, databaseUser, databasePassword );
  20.             statement  = connection.createStatement();
  21.            
  22.         } catch( ClassNotFoundException|SQLException e ) {
  23.             e.printStackTrace();
  24.         }
  25.     }
  26.    
  27.     public void createBookTable() {
  28.         try {
  29.             statement.execute( "DROP TABLE Books" );
  30.             System.out.println( "Dropped Books table" );
  31.         } catch (SQLException e) {
  32.             // If table doesn't exist then don't bother.
  33.         }
  34.        
  35.         try {
  36.             statement.execute(
  37.                 "CREATE TABLE Books ( "             +
  38.                     "Title CHAR(20) NOT NULL, "     +
  39.                     "Price DECIMAL(10,2), "         +
  40.                     "Quantity INT,"                 +
  41.                     "Author CHAR(20),"              +
  42.                     "Publisher CHAR(20),"           +
  43.                     "Category CHAR(20) )"
  44.             );
  45.             System.out.println( "Created Books table" );
  46.         } catch (SQLException e) {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.    
  51.     public void createCDTable() {
  52.         try {
  53.             statement.execute( "DROP TABLE CDs" );
  54.             System.out.println( "Dropped CDs table" );
  55.         } catch (SQLException e) {
  56.             // If table doesn't exist then don't bother.
  57.         }
  58.        
  59.         try {
  60.             statement.execute(
  61.                 "CREATE TABLE CDs ( "                +
  62.                     "Title CHAR(20) NOT NULL, "      +
  63.                     "Price DECIMAL(10,2), "          +
  64.                     "Quantity INT,"                  +
  65.                     "Artist CHAR(20) NOT NULL,"      +
  66.                     "ReleaseDate DATE )"
  67.             );
  68.             System.out.println( "Created CDs table" );
  69.         } catch (SQLException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.    
  74.     public void loadBookTable() {
  75.         try {
  76.             statement.execute(
  77.                 "INSERT INTO Books ( Title, Price, Quantity, Author, Publisher, Category )" +
  78.                 "VALUES ( 'Godzilla On Holiday', 24.95, 3, 'Hiro Grinder', 'Rising Sun Press', 'NON-FICTION' )"
  79.             );
  80.         } catch (SQLException e) {
  81.             e.printStackTrace();
  82.         }
  83.     }
  84.    
  85.     public void loadCDTable() {
  86.         try {
  87.             statement.execute(
  88.                 "INSERT INTO CDs ( Title, Price, Quantity, Artist, ReleaseDate )" +
  89.                 "VALUES ( 'Robot Ensemble', 14.95, 2, 'Cuclulon', '2001-07-06' )"
  90.             );
  91.         } catch (SQLException e) {
  92.             e.printStackTrace();
  93.         }
  94.     }
  95.    
  96.     public List<Book> getBooks() {
  97.         List<Book> books = new LinkedList<>();
  98.         try( ResultSet results = statement.executeQuery( "SELECT * FROM Books" ) ) {
  99.             while( results.next() ) {
  100.                 String title     = results.getString( 1 );
  101.                 double price     = results.getDouble( 2 );
  102.                 int    quant     = results.getInt( 3 );
  103.                 String author    = results.getString( 4 );
  104.                 String publisher = results.getString( 5 );
  105.                 String category  = results.getString( 6 );
  106.                 books.add( new Book( title, price, quant, author, publisher, category ) );
  107.             }
  108.         } catch( SQLException e ) {
  109.             e.printStackTrace();
  110.         }
  111.         return books;
  112.     }
  113. }
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