Advertisement
ssakela

datamodel

Jan 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. package event.handling;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import javafx.collections.FXCollections;
  9. import javafx.collections.ObservableList;
  10. //import event.handling.utils.TextFileReader;
  11.  
  12. //=====================================================================
  13. public class DataModel
  14. {
  15. // --- Public Members -----------------
  16. public static enum Source
  17. {
  18. SRC_NULL, SRC_DBASE
  19. };
  20.  
  21. // --- Private Members ----------------
  22. private ObservableList<Track> the_tracklst = null;
  23. private ObservableList<String> the_statelst = null;
  24. private Source the_src_type = Source.SRC_NULL;
  25.  
  26. // --- Constructors -------------------
  27. public DataModel() // Default
  28. {
  29. the_tracklst = FXCollections.observableArrayList();
  30. the_statelst = FXCollections.observableArrayList();
  31. }
  32.  
  33. public DataModel(Source src_type)
  34. {
  35. this();
  36. this.setSourceType(src_type);
  37. }
  38.  
  39. public DataModel(String source, Source src_type)
  40. {
  41. this();
  42. this.setData(source, src_type);
  43. }
  44.  
  45. // --- Methods ---
  46. private void setSourceType(Source src_type)
  47. {
  48. this.the_src_type = src_type;
  49. }
  50.  
  51. public Source getSourceType()
  52. {
  53. return the_src_type;
  54. }
  55.  
  56. public ObservableList<Track> getCityData()
  57. {
  58. return the_tracklst;
  59. }
  60.  
  61. public ObservableList<String> getStateData()
  62. {
  63. return the_statelst;
  64. }
  65.  
  66. public void setData(String source, DataModel.Source src_type)
  67. {
  68. this.setSourceType(src_type);
  69. this.setData(source);
  70. }
  71.  
  72. public void setData(String source)
  73. {
  74. switch (getSourceType())
  75. {
  76. case SRC_DBASE:
  77. this.loadDbTable(source);
  78. break;
  79. case SRC_NULL:
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85.  
  86.  
  87.  
  88. private final void loadDbTable(String source)
  89. {
  90. final String sql = "SELECT * FROM " + source+ " order by ArtistId ASC;";
  91. try (Connection conn = this.connectDatabase();
  92. Statement stmt = conn.createStatement();
  93. ResultSet rset = stmt.executeQuery(sql))
  94. {
  95. Track record = null;
  96. while (rset.next())
  97. {
  98. record = new Track(rset.getInt("AlbumId"),
  99. rset.getString("Title"),
  100. rset.getInt("ArtistId")
  101. );
  102. the_tracklst.add(record); // Add City object to the list
  103. }
  104. }
  105. catch (SQLException e)
  106. {
  107. // e.printStackTrace();
  108. System.out.println(e.getMessage());
  109. }
  110. }
  111.  
  112. public static void deleteDbEntry(int albumId, String title, int artistId) {
  113. final String sql = "DELETE from albums where albumId = "+albumId;
  114. try {
  115. String db = "jdbc:sqlite:./data/dbs/chinook.db";
  116. Statement st = DriverManager.getConnection(db).createStatement();
  117. st.execute(sql);
  118. } catch (SQLException e)
  119. {
  120. // e.printStackTrace();
  121. System.out.println(e.getMessage());
  122. }
  123. }
  124.  
  125. public static void addDbEntry(int albumId, String title, int artistId) {
  126. final String sql = "insert into albums(Title, ArtistId) values (' " + title + " ' , " + artistId + " ) ";
  127. try {
  128. String db = "jdbc:sqlite:./data/dbs/chinook.db";
  129. Statement st = DriverManager.getConnection(db).createStatement();
  130. st.execute(sql);
  131. } catch (SQLException e)
  132. {
  133. // e.printStackTrace();
  134. System.out.println(e.getMessage());
  135. }
  136. }
  137.  
  138. public void getNewDb() {
  139. loadDbTable("albums");
  140. }
  141.  
  142. public static void editDbEntry(int albumId, String title, int artistId ) {
  143. final String sql = "update albums set Title = " + title + " , ArtistId = " + artistId + " where albumId = "+albumId;
  144. try {
  145. String db = "jdbc:sqlite:./data/dbs/chinook.db";
  146. Statement st = DriverManager.getConnection(db).createStatement();
  147. st.execute(sql);
  148. } catch (SQLException e)
  149. {
  150. // e.printStackTrace();
  151. System.out.println(e.getMessage());
  152. }
  153. }
  154.  
  155. private final Connection connectDatabase()
  156. {
  157. // SQLite connection string
  158. String url = "jdbc:sqlite:./data/dbs/chinook.db";
  159. Connection conn = null;
  160. try
  161. {
  162. conn = DriverManager.getConnection(url);
  163. } catch (SQLException e)
  164. {
  165. System.out.println(e.getMessage());
  166. }
  167. return conn;
  168. }
  169.  
  170. public void close()
  171. {
  172. switch (getSourceType())
  173. {
  174. case SRC_DBASE:
  175. // TODO: Do something with the database
  176. break;
  177. case SRC_NULL:
  178. // TODO: Do nothing
  179. break;
  180. default:
  181. // TODO: Do nothing
  182. break;
  183. }
  184.  
  185. }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement