Advertisement
Darano

JDBCStrategy

Nov 30th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.29 KB | None | 0 0
  1. package server.classes;
  2.  
  3. import server.interfaces.Playlist;
  4. import server.interfaces.SerializableStrategy;
  5. import server.interfaces.Song;
  6. import java.sql.*;
  7.  
  8. import java.io.IOException;
  9. import java.sql.SQLException;
  10.  
  11. public class JDBCStrategy implements server.interfaces.SerializableStrategy{
  12.     private Connection connection;
  13.     private PreparedStatement pst;
  14.     private ResultSet rs;
  15.  
  16.  
  17.     @Override
  18.     public void openWritableLibrary() throws SQLException, IOException {
  19.         connection = DriverManager.getConnection("jdbc:sqlite:lib.db");
  20.         pst = connection.prepareStatement("CREATE TABLE IF NOT EXISTS lib (id long, title text, interpret text, album text, path text)");
  21.         pst.executeUpdate();
  22.     }
  23.  
  24.     @Override
  25.     public void openReadableLibrary() throws SQLException, IOException {
  26.         connection = DriverManager.getConnection("jdbc:sqlite:lib.db");
  27.     }
  28.  
  29.     @Override
  30.     public void openWritablePlaylist() throws SQLException, IOException {
  31.         connection = DriverManager.getConnection("jdbc:sqlite:playlist.db");
  32.         pst = connection.prepareStatement("CREATE TABLE IF NOT EXISTS playlist (id long, title text, interpret text, album text, path text)");
  33.         pst.executeUpdate();
  34.     }
  35.  
  36.     @Override
  37.     public void openReadablePlaylist() throws IOException, SQLException {
  38.         connection = DriverManager.getConnection("jdbc:sqlite:playlist.db");
  39.     }
  40.  
  41.     @Override
  42.     public void writeSong(server.classes.Song s) throws IOException {
  43.         try {
  44.             pst = connection.prepareStatement("INSERT INTO playlist (id, title, interpret, album, path) VALUES (?,?,?,?,?)");
  45.             pst.setLong(1, s.getId());
  46.             pst.setString(2, s.titleProperty().getValue());
  47.             pst.setString(3, s.interpretProperty().getValue());
  48.             pst.setString(4, s.albumProperty().getValue());
  49.             pst.setString(5, s.pathProperty().getValue());
  50.             pst.executeUpdate(); //executeUpdate bei UPDATE, INSERT oder DELETE ; executeQuery bei Rückgabe => SELECT
  51.         } catch (SQLException e) {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public server.classes.Song readSong() throws IOException, ClassNotFoundException, IDOverFlowException {
  58.         server.classes.Song s = new server.classes.Song();
  59.         try {
  60.             pst = connection.prepareStatement("SELECT id, title, interpret, album, path FROM playlist");
  61.             rs = pst.executeQuery();
  62.             while(rs.next()){
  63.                 s.setId(rs.getLong("d"));
  64.                 s.setTitle(rs.getString("title"));
  65.                 s.setInterpret(rs.getString("interpret"));
  66.                 s.setAlbum(rs.getString("album"));
  67.                 s.setPath(rs.getString("path"));
  68.             }
  69.         } catch (SQLException e) {
  70.             e.printStackTrace();
  71.         }
  72.         return s;
  73.     }
  74.  
  75.     @Override
  76.     public void writeLibrary(server.classes.Playlist p) throws IOException {
  77.         for(server.classes.Song s : p){
  78.             try {
  79.                 pst = connection.prepareStatement("INSERT INTO lib (id, title, interpret, album, path) VALUES (?,?,?,?,?)");
  80.                 pst.setLong(1, s.getId());
  81.                 pst.setString(2, s.titleProperty().getValue());
  82.                 pst.setString(3, s.interpretProperty().getValue());
  83.                 pst.setString(4, s.albumProperty().getValue());
  84.                 pst.setString(5, s.pathProperty().getValue());
  85.                 pst.executeUpdate(); //executeUpdate bei UPDATE, INSERT oder DELETE ; executeQuery bei Rückgabe => SELECT
  86.  
  87.             } catch (SQLException e) {
  88.                 e.printStackTrace();
  89.             }
  90.         }
  91.     }
  92.  
  93.     @Override
  94.     public server.classes.Playlist readLibrary() throws IOException, ClassNotFoundException {
  95.         server.classes.Playlist p1 = new server.classes.Playlist();
  96.         try {
  97.             pst = connection.prepareStatement("SELECT * FROM lib");
  98.             rs = pst.executeQuery();
  99.  
  100.             while(rs.next()){
  101.                 server.classes.Song s = new server.classes.Song();
  102.                 s.setId(rs.getLong("id"));
  103.                 s.setAlbum(rs.getString("album"));
  104.                 s.setInterpret(rs.getString("interpret"));
  105.                 s.setTitle(rs.getString("title"));
  106.                 s.setPath(rs.getString("path"));
  107.                 p1.addSong(s);
  108.             }
  109.  
  110.         } catch (SQLException e) {
  111.             e.printStackTrace();
  112.         } catch (IDOverFlowException e) {
  113.             e.printStackTrace();
  114.         }
  115.         return p1;
  116.     }
  117.  
  118.     @Override
  119.     public void writePlaylist(server.classes.Playlist p) throws IOException {
  120.         for(Song s : p){
  121.             try {
  122.                 pst = connection.prepareStatement("INSERT INTO playlist (id, title, interpret, album, path) VALUES (?,?,?,?,?)");
  123.                 pst.setLong(1, s.getId());
  124.                 pst.setString(2, s.titleProperty().getValue());
  125.                 pst.setString(3, s.interpretProperty().getValue());
  126.                 pst.setString(4, s.albumProperty().getValue());
  127.                 pst.setString(5, s.pathProperty().getValue());
  128.                 pst.executeUpdate(); //executeUpdate bei UPDATE, INSERT oder DELETE ; executeQuery bei Rückgabe => SELECT
  129.  
  130.             } catch (SQLException e) {
  131.                 e.printStackTrace();
  132.             }
  133.         }
  134.  
  135.  
  136.     }
  137.  
  138.     @Override
  139.     public server.classes.Playlist readPlaylist() throws IOException, ClassNotFoundException {
  140.         server.classes.Playlist p1 = new server.classes.Playlist();
  141.         try {
  142.             pst = connection.prepareStatement("SELECT * FROM playlist");
  143.             rs = pst.executeQuery();
  144.  
  145.             while (rs.next()) {
  146.                 server.classes.Song s = new server.classes.Song();
  147.                 s.setId(rs.getLong("id"));
  148.                 s.setAlbum(rs.getString("album"));
  149.                 s.setInterpret(rs.getString("interpret"));
  150.                 s.setTitle(rs.getString("title"));
  151.                 s.setPath(rs.getString("path"));
  152.                 p1.addSong(s);
  153.             }
  154.  
  155.         } catch (SQLException e) {
  156.             e.printStackTrace();
  157.         } catch (IDOverFlowException e) {
  158.             e.printStackTrace();
  159.         }
  160.         return p1;
  161.     }
  162.  
  163.     @Override
  164.     public void closeWritableLibrary() {
  165.         if(connection != null){
  166.             try {
  167.                 rs.close();
  168.                 pst.close();
  169.                 connection.close();
  170.             } catch (SQLException e) {
  171.                 e.printStackTrace();
  172.             }
  173.         }
  174.     }
  175.  
  176.     @Override
  177.     public void closeReadableLibrary() {
  178.         if(connection != null){
  179.             try {
  180.                 rs.close();
  181.                 pst.close();
  182.                 rs.close();
  183.                 connection.close();
  184.             } catch (SQLException e) {
  185.                 e.printStackTrace();
  186.             }
  187.         }
  188.     }
  189.  
  190.     @Override
  191.     public void closeWritablePlaylist() {
  192.         if(connection != null){
  193.             try {
  194.  
  195.                 pst.close();
  196.                 rs.close();
  197.                 connection.close();
  198.             } catch (SQLException e) {
  199.                 e.printStackTrace();
  200.             }
  201.         }
  202.     }
  203.  
  204.     @Override
  205.     public void closeReadablePlaylist() {
  206.         if(connection != null){
  207.             try {
  208.                 pst.close();
  209.                 connection.close();
  210.                 rs.close();
  211.             } catch (SQLException e) {
  212.                 e.printStackTrace();
  213.             }
  214.  
  215.         }
  216.     }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement