Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###############################
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GENREDAO
- ###############################
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package daos;
- import dtos.Genre;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- /**
- *
- * @author Team GIT
- */
- public class GenreDao extends Dao implements GenreDaoInterface{
- public GenreDao(String databaseName) {
- super(databaseName);
- }
- /**
- * Gets all genres from the database
- *
- * @return Arraylist containing all genre objects
- */
- @Override
- public ArrayList<Genre> selectAllGenres() {
- //<editor-fold defaultstate="collapsed" desc="SELECT ALL GENRES">
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- ArrayList<Genre> genres = new ArrayList();
- try {
- con = getConnection();
- String query = "SELECT * FROM genres";
- ps = con.prepareStatement(query);
- rs = ps.executeQuery();
- while (rs.next()) {
- Genre g = new Genre(
- rs.getInt("genreId"),
- rs.getString("genreType")
- );
- genres.add(g);
- }
- } catch (SQLException e) {
- System.out.println("Exception occured in the selectAllGenres() method: " + e.getMessage());
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (ps != null) {
- ps.close();
- }
- if (con != null) {
- freeConnection(con);
- }
- } catch (SQLException e) {
- System.out.println("Exception occured in the finally section of the selectAllGenres() method: " + e.getMessage());
- }
- }
- return genres;
- //</editor-fold>
- }
- /**
- * Gets a genre using its Id
- *
- * @param genreId Id for the genre to select
- * @return Genre object with the Id entered
- */
- @Override
- public Genre getGenreByID(int genreId) {
- //<editor-fold defaultstate="collapsed" desc="SELECT GENRE BY ID">
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- Genre g = null;
- try {
- con = getConnection();
- String query = "SELECT * FROM genres WHERE genreId = ?";
- ps = con.prepareStatement(query);
- ps.setInt(1, genreId);
- rs = ps.executeQuery();
- if (rs.next()) {
- g = new Genre(
- rs.getInt("genreId"),
- rs.getString("genreType")
- );
- }
- } catch (SQLException e) {
- System.out.println("Exception occured in the getGenreByID() method: " + e.getMessage());
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (ps != null) {
- ps.close();
- }
- if (con != null) {
- freeConnection(con);
- }
- } catch (SQLException e) {
- System.out.println("Exception occured in the finally section of the getGenreByID() method: " + e.getMessage());
- }
- }
- return g;
- //</editor-fold>
- }
- /**
- * Gets a genretype
- *
- * @return String with genretype
- */
- @Override
- public String getGenreType() {
- //<editor-fold defaultstate="collapsed" desc="GET GENRETYPE">
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- String g = null;
- try {
- con = getConnection();
- String query = "SELECT genreType FROM genres";
- ps = con.prepareStatement(query);
- rs = ps.executeQuery();
- g = (rs.getString("genreType"));
- } catch (SQLException e) {
- System.out.println("Exception occured in the getGenreType() method: " + e.getMessage());
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (ps != null) {
- ps.close();
- }
- if (con != null) {
- freeConnection(con);
- }
- } catch (SQLException e) {
- System.out.println("Exception occured in the finally section of the getGenreType() method: " + e.getMessage());
- }
- }
- return g;
- //</editor-fold>
- }
- }
- ###############################
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GENREDAOTEST
- ###############################
- ///*
- // * To change this license header, choose License Headers in Project Properties.
- // * To change this template file, choose Tools | Templates
- // * and open the template in the editor.
- // */
- //package daos;
- //
- //import dtos.Genre;
- //import java.util.ArrayList;
- //import junit.framework.TestCase;
- //
- ///**
- // *
- // * @author Gavin
- // */
- //public class GenreDaoTest extends TestCase {
- // private static GenreDao genreDao;
- // public GenreDaoTest(String testName) {
- // super(testName);
- //
- // genreDao = new GenreDao("testdb");
- // }
- //
- // @Override
- // protected void setUp() throws Exception {
- // super.setUp();
- // }
- //
- // @Override
- // protected void tearDown() throws Exception {
- // super.tearDown();
- // }
- //
- // /**
- // * Test of selectAllGenres method, of class GenreDao.
- // */
- // public void testSelectAllGenres() {
- // System.out.println("selectAllGenres");
- // GenreDao genreDaoExp = new GenreDao("testdb");
- // ArrayList<Genre> expResult = genreDaoExp.selectAllGenres();
- // ArrayList<Genre> result = genreDao.selectAllGenres();
- // assertEquals(expResult, result);
- // }
- //
- // /**
- // * Test of getGenreByID method, of class GenreDao.
- // */
- // public void testGetGenreByID() {
- // System.out.println("getGenreByID");
- // int genreId = 1;
- // Genre expResult = new Genre(1,"Action");
- // Genre result = genreDao.getGenreByID(genreId);
- // assertEquals(expResult, result);
- // }
- //
- // /**
- // * Test of getGenreType method, of class GenreDao.
- // */
- // public void testGetGenreType() {
- // System.out.println("getGenreType");
- // String expResult = "Action";
- // String result = genreDao.getGenreByID(1).getGenreType();
- // assertEquals(expResult, result);
- // }
- //
- //}
- package daos;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /**
- *
- * @author Team GIT
- */
- public class Dao {
- private String databaseName;
- public Dao(String databaseName) {
- this.databaseName = databaseName;
- }
- /**
- * Provides connection to the database
- *
- * @return con
- */
- public Connection getConnection() {
- // Had to add the code after databaseName because it kept telling me something is wrong with the timezone
- String driver = "com.mysql.cj.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/" + databaseName + "?characterEncoding=utf8&useUnicode=true&sessionVariables=storage_engine%3DInnoDB&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- String username = "root";
- String password = "";
- Connection con = null;
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(url, username, password);
- } catch (ClassNotFoundException ex1) {
- System.out.println("Failed to find driver class " + ex1.getMessage());
- System.exit(1);
- } catch (SQLException ex2) {
- System.out.println("Connection failed " + ex2.getMessage());
- }
- return con;
- }
- /**
- * Clears the connection to the database
- *
- * @param con
- * @return void
- */
- public void freeConnection(Connection con) {
- try {
- if (con != null) {
- con.close();
- con = null;
- }
- } catch (SQLException e) {
- System.out.println("Failed to free connection: " + e.getMessage());
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement