Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. public class DBStudyCourseSerializer implements ISerializer<IStudyCourse> {
  2.  
  3. String driver = null;
  4.  
  5. // --------------------------------------------------------------------------
  6.  
  7. String host = null;
  8.  
  9. String database = null;
  10.  
  11. String port = null;
  12.  
  13. String user = null;
  14.  
  15. String password = null;
  16.  
  17. Connection connection = null;
  18.  
  19.  
  20. // --------------------------------------------------------------------------
  21.  
  22. public DBStudyCourseSerializer(String jdbcDriverClass, String dbUrl,
  23. String user, String pw){
  24.  
  25. driver = jdbcDriverClass;
  26. host = dbUrl;
  27. this.user = user;
  28. password=pw;
  29.  
  30. loadJdbcDriver ();
  31. openConnection ();
  32.  
  33. try
  34. {
  35. Statement statement = connection.createStatement ();
  36.  
  37. try
  38. { String str1 = "DROP TABLE studycourses;";
  39. statement.executeUpdate (str1);
  40. }
  41. catch (SQLException e)
  42. {
  43. }
  44.  
  45. String str2 = "CREATE TABLE studycourses" +
  46. "(id integer PRIMARY KEY," +
  47. "type char(20)," +
  48. "title char(20)," +
  49. "lecturer integer," +
  50. "room integer," +
  51. "maxstuds integer," +
  52. "description char(100)," +
  53. "lecture integer);";
  54. statement.executeUpdate (str2);
  55.  
  56. try{
  57. String str3 = "DROP TABLE study_times";
  58. statement.executeUpdate (str3);
  59. }catch(SQLException e){
  60. }
  61. try{
  62. String str4 = "CREATE TABLE study_times" +
  63. "(SC_ID integer," +
  64. "TS_ID integer," +
  65. "PRIMARY KEY (SC_ID, TS_ID));";
  66. statement.executeUpdate (str4);
  67. }catch(SQLException e){
  68. }
  69. statement.close ();
  70. close();
  71. }catch(SQLException e){
  72. e.printStackTrace ();
  73. }
  74. }
  75.  
  76. @Override
  77. public void close() {
  78. try
  79. {
  80. connection.close ();
  81. }
  82. catch (SQLException e)
  83. {
  84. e.printStackTrace ();
  85. System.exit (1);
  86. }
  87.  
  88. System.out.println ("\nconnection closed");
  89. }
  90.  
  91. @Override
  92. public void serialize(IStudyCourse study) {
  93. try
  94. {
  95. loadJdbcDriver ();
  96. openConnection ();
  97. int lec = -1;
  98. String type=null;
  99. String str=null;
  100. Statement statement = connection.createStatement ();
  101.  
  102. if (study instanceof ILecture) {
  103. type = "ILecture";
  104. } else if (study instanceof ISeminar) {
  105. type = "ISeminar";
  106. } else if (study instanceof IExercise) {
  107. type = "IExercise";
  108. lec=((IExercise)study).getLecture().getID();
  109. }
  110. if(lec==-1){
  111. str = "INSERT INTO studycourses " +
  112. "VALUES("+study.getID()+",'"+type+"','"+study.getTitle()+"',"+study.getLecturer().getID()+","+study.getRoom().getID()+
  113. ","+study.getMaxStudents()+",'"+study.getDescription()+"',null);";
  114. }else{
  115. str = "INSERT INTO studycourses " +
  116. "VALUES("+study.getID()+",'"+type+"','"+study.getTitle()+"',"+study.getLecturer().getID()+","+study.getRoom().getID()+
  117. ","+study.getMaxStudents()+",'"+study.getDescription()+"',"+lec+");";
  118. }
  119. statement.executeUpdate (str);
  120.  
  121. statement.close ();
  122. }
  123. catch (SQLException e)
  124. {
  125. e.printStackTrace ();
  126. }
  127. try
  128. {
  129. for(ITimeSlot ts : study.getTimeSlots()){
  130. Statement statement = connection.createStatement ();
  131. String str5 = "INSERT INTO study_times " +
  132. "VALUES("+study.getID()+","+ts.getID()+");";
  133.  
  134. statement.executeUpdate (str5);
  135.  
  136. statement.close ();
  137. }
  138. }
  139. catch (SQLException e)
  140. {
  141. }
  142. close();
  143. }
  144.  
  145. @Override
  146. public void serialize(List<IStudyCourse> studys) {
  147.  
  148. for(IStudyCourse tss : studys){
  149. serialize(tss);
  150. }
  151. }
  152.  
  153. private void openConnection ()
  154. {
  155. try
  156. {
  157. connection = DriverManager.getConnection (host,
  158. user,
  159. password);
  160. }
  161. catch (SQLException e)
  162. {
  163. e.printStackTrace ();
  164. System.exit (1);
  165. }
  166.  
  167. System.out.println ("connection opened");
  168. }
  169.  
  170. private void loadJdbcDriver ()
  171. {
  172. try
  173. {
  174. Class.forName (driver);
  175. }
  176. catch (ClassNotFoundException e)
  177. {
  178. e.printStackTrace ();
  179. System.exit (1);
  180. }
  181.  
  182. System.out.println ("driver loaded");
  183. }
  184.  
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement