Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.79 KB | None | 0 0
  1. ###############################
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GENREDAO
  3. ###############################
  4. /*
  5.  * To change this license header, choose License Headers in Project Properties.
  6.  * To change this template file, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9. package daos;
  10.  
  11. import dtos.Genre;
  12. import java.sql.Connection;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.util.ArrayList;
  17.  
  18. /**
  19.  *
  20.  * @author Team GIT
  21.  */
  22. public class GenreDao extends Dao implements GenreDaoInterface{
  23.  
  24.     public GenreDao(String databaseName) {
  25.         super(databaseName);
  26.     }
  27.    
  28.     /**
  29.      * Gets all genres from the database
  30.      *
  31.      * @return Arraylist containing all genre objects
  32.      */
  33.     @Override
  34.     public ArrayList<Genre> selectAllGenres() {
  35.         //<editor-fold defaultstate="collapsed" desc="SELECT ALL GENRES">
  36.         Connection con = null;
  37.         PreparedStatement ps = null;
  38.         ResultSet rs = null;
  39.         ArrayList<Genre> genres = new ArrayList();
  40.  
  41.         try {
  42.             con = getConnection();
  43.  
  44.             String query = "SELECT * FROM genres";
  45.             ps = con.prepareStatement(query);
  46.             rs = ps.executeQuery();
  47.  
  48.             while (rs.next()) {
  49.                 Genre g = new Genre(
  50.                         rs.getInt("genreId"),
  51.                         rs.getString("genreType")
  52.                 );
  53.                 genres.add(g);
  54.             }
  55.         } catch (SQLException e) {
  56.             System.out.println("Exception occured in the selectAllGenres() method: " + e.getMessage());
  57.         } finally {
  58.             try {
  59.                 if (rs != null) {
  60.                     rs.close();
  61.                 }
  62.                 if (ps != null) {
  63.                     ps.close();
  64.                 }
  65.                 if (con != null) {
  66.                     freeConnection(con);
  67.                 }
  68.             } catch (SQLException e) {
  69.                 System.out.println("Exception occured in the finally section of the selectAllGenres() method: " + e.getMessage());
  70.             }
  71.         }
  72.  
  73.         return genres;
  74. //</editor-fold>
  75.     }
  76.    
  77.     /**
  78.      * Gets a genre using its Id
  79.      *
  80.      * @param genreId Id for the genre to select
  81.      * @return Genre object with the Id entered
  82.      */
  83.     @Override
  84.     public Genre getGenreByID(int genreId) {
  85.         //<editor-fold defaultstate="collapsed" desc="SELECT GENRE BY ID">
  86.         Connection con = null;
  87.         PreparedStatement ps = null;
  88.         ResultSet rs = null;
  89.         Genre g = null;
  90.  
  91.         try {
  92.             con = getConnection();
  93.  
  94.             String query = "SELECT * FROM genres WHERE genreId = ?";
  95.             ps = con.prepareStatement(query);
  96.             ps.setInt(1, genreId);
  97.             rs = ps.executeQuery();
  98.  
  99.             if (rs.next()) {
  100.                 g = new Genre(
  101.                         rs.getInt("genreId"),
  102.                         rs.getString("genreType")
  103.                 );
  104.             }
  105.         } catch (SQLException e) {
  106.             System.out.println("Exception occured in the getGenreByID() method: " + e.getMessage());
  107.         } finally {
  108.             try {
  109.                 if (rs != null) {
  110.                     rs.close();
  111.                 }
  112.                 if (ps != null) {
  113.                     ps.close();
  114.                 }
  115.                 if (con != null) {
  116.                     freeConnection(con);
  117.                 }
  118.             } catch (SQLException e) {
  119.                 System.out.println("Exception occured in the finally section of the getGenreByID() method: " + e.getMessage());
  120.             }
  121.         }
  122.  
  123.         return g;
  124. //</editor-fold>
  125.     }
  126.    
  127.     /**
  128.      * Gets a genretype
  129.      *
  130.      * @return String with genretype
  131.      */
  132.     @Override
  133.     public String getGenreType() {
  134.         //<editor-fold defaultstate="collapsed" desc="GET GENRETYPE">
  135.         Connection con = null;
  136.         PreparedStatement ps = null;
  137.         ResultSet rs = null;
  138.         String g = null;
  139.  
  140.         try {
  141.             con = getConnection();
  142.  
  143.             String query = "SELECT genreType FROM genres";
  144.             ps = con.prepareStatement(query);
  145.             rs = ps.executeQuery();
  146.  
  147.                 g = (rs.getString("genreType"));
  148.                
  149.         } catch (SQLException e) {
  150.             System.out.println("Exception occured in the getGenreType() method: " + e.getMessage());
  151.         } finally {
  152.             try {
  153.                 if (rs != null) {
  154.                     rs.close();
  155.                 }
  156.                 if (ps != null) {
  157.                     ps.close();
  158.                 }
  159.                 if (con != null) {
  160.                     freeConnection(con);
  161.                 }
  162.             } catch (SQLException e) {
  163.                 System.out.println("Exception occured in the finally section of the getGenreType() method: " + e.getMessage());
  164.             }
  165.         }
  166.  
  167.         return g;
  168. //</editor-fold>
  169.     }
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. ###############################
  179. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GENREDAOTEST
  180. ###############################
  181. ///*
  182. // * To change this license header, choose License Headers in Project Properties.
  183. // * To change this template file, choose Tools | Templates
  184. // * and open the template in the editor.
  185. // */
  186. //package daos;
  187. //
  188. //import dtos.Genre;
  189. //import java.util.ArrayList;
  190. //import junit.framework.TestCase;
  191. //
  192. ///**
  193. // *
  194. // * @author Gavin
  195. // */
  196. //public class GenreDaoTest extends TestCase {
  197. //    private static GenreDao genreDao;
  198. //    public GenreDaoTest(String testName) {
  199. //        super(testName);
  200. //        
  201. //        genreDao = new GenreDao("testdb");
  202. //    }
  203. //    
  204. //    @Override
  205. //    protected void setUp() throws Exception {
  206. //        super.setUp();
  207. //    }
  208. //    
  209. //    @Override
  210. //    protected void tearDown() throws Exception {
  211. //        super.tearDown();
  212. //    }
  213. //
  214. //    /**
  215. //     * Test of selectAllGenres method, of class GenreDao.
  216. //     */
  217. //    public void testSelectAllGenres() {
  218. //        System.out.println("selectAllGenres");
  219. //        GenreDao genreDaoExp = new GenreDao("testdb");
  220. //        ArrayList<Genre> expResult = genreDaoExp.selectAllGenres();
  221. //        ArrayList<Genre> result = genreDao.selectAllGenres();
  222. //        assertEquals(expResult, result);
  223. //    }
  224. //
  225. //    /**
  226. //     * Test of getGenreByID method, of class GenreDao.
  227. //     */
  228. //    public void testGetGenreByID() {
  229. //        System.out.println("getGenreByID");
  230. //        int genreId = 1;
  231. //        Genre expResult = new Genre(1,"Action");
  232. //        Genre result = genreDao.getGenreByID(genreId);
  233. //        assertEquals(expResult, result);
  234. //    }
  235. //
  236. //    /**
  237. //     * Test of getGenreType method, of class GenreDao.
  238. //     */
  239. //    public void testGetGenreType() {
  240. //        System.out.println("getGenreType");
  241. //        String expResult = "Action";
  242. //        String result = genreDao.getGenreByID(1).getGenreType();
  243. //        assertEquals(expResult, result);
  244. //    }
  245. //    
  246. //}
  247.  
  248.  
  249. package daos;
  250.  
  251. import java.sql.Connection;
  252. import java.sql.DriverManager;
  253. import java.sql.SQLException;
  254.  
  255. /**
  256.  *
  257.  * @author Team GIT
  258.  */
  259. public class Dao {
  260.  
  261.     private String databaseName;
  262.  
  263.     public Dao(String databaseName) {
  264.         this.databaseName = databaseName;
  265.     }
  266.  
  267.     /**
  268.      * Provides connection to the database
  269.      *
  270.      * @return con
  271.      */
  272.     public Connection getConnection() {
  273.         // Had to add the code after databaseName because it kept telling me something is wrong with the timezone
  274.         String driver = "com.mysql.cj.jdbc.Driver";
  275.         String url = "jdbc:mysql://localhost:3306/" + databaseName + "?characterEncoding=utf8&useUnicode=true&sessionVariables=storage_engine%3DInnoDB&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  276.         String username = "root";
  277.         String password = "";
  278.         Connection con = null;
  279.         try {
  280.             Class.forName(driver);
  281.             con = DriverManager.getConnection(url, username, password);
  282.         } catch (ClassNotFoundException ex1) {
  283.             System.out.println("Failed to find driver class " + ex1.getMessage());
  284.             System.exit(1);
  285.         } catch (SQLException ex2) {
  286.             System.out.println("Connection failed " + ex2.getMessage());
  287.         }
  288.         return con;
  289.     }
  290.  
  291.     /**
  292.      * Clears the connection to the database
  293.      *
  294.      * @param con
  295.      * @return void
  296.      */
  297.     public void freeConnection(Connection con) {
  298.         try {
  299.             if (con != null) {
  300.                 con.close();
  301.                 con = null;
  302.             }
  303.         } catch (SQLException e) {
  304.             System.out.println("Failed to free connection: " + e.getMessage());
  305.             System.exit(1);
  306.         }
  307.     }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement