Sei sulla pagina 1di 3

public Album getDetailedAlbum(int albumId) { Album a = new Album(); /* * Query the database and get the album with

the * specified ID, create an Album object with all * details specified */ try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException e) { System.out.println("InstantiationException: "); e.printStackTrace(); throw new RuntimeException(e); } catch (IllegalAccessException e) { System.out.println("IllegalAccessException: "); e.printStackTrace(); throw new RuntimeException(e); } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException: "); e.printStackTrace(); throw new RuntimeException(e); } Connection conn = null; try { conn = DriverManager.getConnection(DatabaseInfo.url, Dat abaseInfo.username, DatabaseInfo.password); } catch (SQLException e) { System.out.println("SQLException: "); e.printStackTrace(); throw new RuntimeException(e); } String query = "SELECT AlbumTitle, AlbumYear, BandID, AlbumNumbe r FROM Album where AlbumID = ?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(query); ps.setInt(1, albumId); rs = ps.executeQuery(); if(rs.next()) { Band bands = new Band(); a.setAlbumId(albumId); a.setTitle(rs.getString("AlbumTitle")); a.setYear(rs.getInt("AlbumYear")); bands.setBandId(rs.getInt("BandID")); a.setBand(bands); a.setAlbumNumber(rs.getInt("AlbumNumber")); } rs.close(); } catch (SQLException e) { System.out.println("SQLException: "); e.printStackTrace();

throw new RuntimeException(e); } try { if(rs != null && !rs.isClosed()) rs.close(); if(ps != null && !ps.isClosed()) ps.close(); if(conn != null && !conn.isClosed()) conn.close(); } catch (SQLException e) { System.out.println("SQLException: "); e.printStackTrace(); throw new RuntimeException(e); } return a; } public List<Album> getAlbums() { List<Album> albums = new ArrayList<Album>(); /* * Query the database and get all the albums in the * database with all the necessary fields, then construct * Album objects and add them to the albums array */ try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException e) { System.out.println("InstantiationException: "); e.printStackTrace(); throw new RuntimeException(e); } catch (IllegalAccessException e) { System.out.println("IllegalAccessException: "); e.printStackTrace(); throw new RuntimeException(e); } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException: "); e.printStackTrace(); throw new RuntimeException(e); } Connection conn = null; try { conn = DriverManager.getConnection(DatabaseInfo.url, Dat abaseInfo.username, DatabaseInfo.password); } catch (SQLException e) { System.out.println("SQLException: "); e.printStackTrace(); throw new RuntimeException(e); } String query = "SELECT AlbumID, AlbumTitle, AlbumYear, BandID, A lbumNumber FROM Albums"; PreparedStatement ps = null;

ResultSet rs = null; try { ps = conn.prepareStatement(query); rs = ps.executeQuery(); if(rs.next()) { Album a = new Album(); Band bands = new Band(); a.setAlbumId(rs.getInt("AlbumID")); a.setTitle(rs.getString("AlbumTitle")); a.setYear(rs.getInt("AlbumYear")); bands.setBandId(rs.getInt("BandID")); a.setBand(bands); a.setAlbumNumber(rs.getInt("AlbumNumber")); albums.add(a); } rs.close(); } catch (SQLException e) { System.out.println("SQLException: "); e.printStackTrace(); throw new RuntimeException(e); } try { if(rs != null && !rs.isClosed()) rs.close(); if(ps != null && !ps.isClosed()) ps.close(); if(conn != null && !conn.isClosed()) conn.close(); } catch (SQLException e) { System.out.println("SQLException: "); e.printStackTrace(); throw new RuntimeException(e); } return albums; }

Potrebbero piacerti anche