Guest User

Untitled

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