Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package wk6;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10.  
  11.  
  12. public class DVDDao {
  13.    
  14.     public DVDDao() {}
  15.    
  16.     private static Connection getDBConnection() {
  17.         Connection dbConnection = null;
  18.         try {
  19.             Class.forName("org.sqlite.JDBC");
  20.             } catch (ClassNotFoundException e) {
  21.                 System.out.println(e.getMessage());
  22.             }
  23.         try {
  24.             String dbURL = "jdbc:sqlite:dvd.sqlite";
  25.             dbConnection = DriverManager.getConnection(dbURL);
  26.             return dbConnection;
  27.             } catch (SQLException e) {
  28.                 System.out.println(e.getMessage());
  29.             }
  30.         return dbConnection;
  31.         }
  32.    
  33.     public ArrayList<dvd> getAllDVDs() throws SQLException {
  34.         System.out.println("Retrieving all dvds ...");
  35.         Connection dbConnection = null;
  36.         Statement statement = null;
  37.         ResultSet result = null;
  38.         String query = "SELECT * FROM collection;";
  39.         ArrayList<dvd> dvds = new ArrayList<>();
  40.  
  41.         try {
  42.             dbConnection = getDBConnection();
  43.             statement = dbConnection.createStatement();
  44.             System.out.println("DBQuery = " + query);
  45.             result = statement.executeQuery(query); // Execute SQL query and record response to string
  46.             while (result.next()) {
  47.  
  48.                 int id = result.getInt("ID");
  49.                 String title = result.getString("Title");
  50.                 String genre = result.getString("Genre");
  51.                 int year = result.getInt("Year");
  52.                
  53.                 dvds.add(new dvd(id,title,genre,year));
  54.             }
  55.         } catch(Exception e) {
  56.             System.out.println("get all dvds: "+e);
  57.         } finally {
  58.             if (result != null) {
  59.                 result.close();
  60.             }
  61.             if (statement != null) {
  62.                 statement.close();
  63.             }
  64.             if (dbConnection != null) {
  65.                 dbConnection.close();
  66.             }
  67.         }
  68.         return dvds;
  69.     }
  70.  
  71.     public dvd getDVD(int film_id) throws SQLException {
  72.  
  73.         dvd temp = null;
  74.         Connection dbConnection = null;
  75.         Statement statement = null;
  76.         ResultSet result = null;
  77.  
  78.         String query = "SELECT * FROM collection WHERE ID =" + film_id + ";";
  79.  
  80.         try {
  81.             dbConnection = getDBConnection();
  82.             statement = dbConnection.createStatement();
  83.             System.out.println("DBQuery: " + query);
  84.             // execute SQL query
  85.             result = statement.executeQuery(query);
  86.  
  87.             while (result.next()) {
  88.  
  89.  
  90.                 int id = result.getInt("ID");
  91.                 String title = result.getString("Title");
  92.                 String genre = result.getString("Genre");
  93.                 int year = result.getInt("Year");
  94.                
  95.                 temp = new dvd(id,title,genre,year);
  96.  
  97.             }
  98.         } finally {
  99.             if (result != null) {
  100.                 result.close();
  101.             }
  102.             if (statement != null) {
  103.                 statement.close();
  104.             }
  105.             if (dbConnection != null) {
  106.                 dbConnection.close();
  107.             }
  108.         }
  109.         return temp;
  110.     }
  111.  
  112.     public Boolean deleteDVD(int film_id) throws SQLException {
  113.         System.out.println("Deleting dvd");
  114.         Connection dbConnection = null;
  115.         Statement statement = null;
  116.         int result = 0;
  117.         String query = "DELETE FROM collection WHERE ID = " + film_id + ";";
  118.         try {
  119.             dbConnection = getDBConnection();
  120.             statement = dbConnection.createStatement();
  121.             System.out.println(query);
  122.             // execute SQL query
  123.             result = statement.executeUpdate(query);
  124.         } finally {
  125.             if (statement != null) {
  126.                 statement.close();
  127.             }
  128.             if (dbConnection != null) {
  129.                 dbConnection.close();
  130.             }
  131.         }
  132.         if (result == 1) {
  133.             return true;
  134.         } else {
  135.             return false;
  136.         }
  137.     }
  138.  
  139.     public Boolean updateDVD(dvd dvd) throws SQLException {
  140.         Connection dbConnection = null;
  141.         Statement statement = null;
  142.  
  143.         String query = "UPDATE collection " + "SET ID = '" + dvd.getId() + "'," + "Title = '"
  144.                 + dvd.getTitle() + "'," + "Genre= '" + dvd.getGenre() + "'," + "Year= '" + dvd.getYear() +" WHERE ID = " + dvd.getId()
  145.                 + ";";
  146.  
  147.         try {
  148.             dbConnection = getDBConnection();
  149.             statement = dbConnection.createStatement();
  150.             System.out.println(query);
  151.             // execute SQL update
  152.             statement.executeUpdate(query);
  153.  
  154.         } catch (SQLException e) {
  155.  
  156.             System.out.println(e.getMessage());
  157.             return false;
  158.  
  159.         } finally {
  160.  
  161.             if (statement != null) {
  162.                 statement.close();
  163.             }
  164.             if (dbConnection != null) {
  165.                 dbConnection.close();
  166.             }
  167.         }
  168.         return true;
  169.     }
  170.  
  171.     public boolean insertRecordIntoCollectionTable(dvd in) throws SQLException{
  172.         Connection dbConnection = null;
  173.         Statement statement = null;
  174.        
  175.         String update = "INSERT INTO collection (ID, Title, Genre, Year) VALUES ("+in.getId()+",'"+in.getTitle()+"','"+in.getGenre()+"',"+in.getYear()+");";
  176.         boolean ok = false;
  177.             try {
  178.                     dbConnection = getDBConnection();
  179.                     statement = dbConnection.createStatement();
  180.                     System.out.println(update);
  181.         // execute SQL query
  182.                     statement.executeUpdate(update);
  183.                     ok = true;
  184.                 } catch (SQLException e) {
  185.                     System.out.println(e.getMessage());
  186.                 } finally {
  187.                     if (statement != null) {
  188.                         statement.close();
  189.                     }
  190.                     if (dbConnection != null) {
  191.                         dbConnection.close();
  192.                     }
  193.                    
  194.                 }
  195.             return ok;
  196.     }
  197.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement