SQLConnector.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package data.io;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.List;
  8. import model.objects.Highscore;
  9. import audio.Song;
  10. import audio.SongInstance;
  11. public class SQLConnector
  12. {
  13. private Connection myConn = null;
  14. public SQLConnector()
  15. {
  16. this("178.62.254.153", "3306","colorstrike","csadmin","aardbei123");
  17. }
  18. //Start connection with Databse
  19. public SQLConnector(String host, String port, String dbName, String userName, String password)
  20. {
  21. try
  22. {
  23. String url = "jdbc:mysql://" + host + ":" + port + "/"+ dbName + "?user="
  24. + userName
  25. + "&password="
  26. + password;
  27. Class.forName("com.mysql.jdbc.Driver").newInstance ();
  28. myConn = DriverManager.getConnection(url);
  29. }
  30. catch( SQLException ex)
  31. {
  32. System.out.println("SQLException: " + ex.getMessage());
  33. System.out.println("SQLState: " + ex.getSQLState());
  34. System.out.println("VendorError: " + ex.getErrorCode());
  35. }
  36. catch(Exception ex)
  37. {
  38. System.out.println("Error : " + ex.getMessage());
  39. }
  40. }
  41. public List<Highscore> getHighscore(String song)
  42. {
  43. Statement st = executeResultQuery("SELECT * FROM highscore");
  44. ResultSet result = null;
  45. try {
  46. result = st.getResultSet();
  47. } catch (SQLException e1) {
  48. e1.printStackTrace();
  49. }
  50. try {
  51. while(result.next())
  52. {
  53. }
  54. } catch (SQLException e) {
  55. }
  56. return null;
  57. }
  58. public void update(List<Song> songs)
  59. {
  60. for(Song s : songs)
  61. {
  62. executeInsertQuery("INSERT INTO song (folder, title, author) VALUES ('" + s.getFolder() + "', '" + s.getTitle() + "', '" + s.getAuthor() + "');");
  63. Statement st = executeResultQuery("SELECT id FROM song WHERE folder='" + s.getFolder() + "'");
  64. ResultSet rs = null;
  65. int id = -1;
  66. try {
  67. rs = st.getResultSet();
  68. rs.next();
  69. id = rs.getInt(1);
  70. System.out.println("Song " + id + " added to database");
  71. rs.close();
  72. st.close();
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. }
  76. if(id != -1)
  77. {
  78. for(SongInstance si : s.getSongs())
  79. {
  80. Statement st2 = executeResultQuery("SELECT id FROM songinstance WHERE song=" + id + " AND difficulty='" + si.getDifficulty() + "'");
  81. ResultSet rs2 = null;
  82. try {
  83. rs2 = st2.getResultSet();
  84. if(rs2.first())
  85. {
  86. System.err.println("SongInstance Already Exists ");
  87. }
  88. else
  89. {
  90. executeInsertQuery("INSERT INTO songinstance (song, enemies, difficulty) VALUES (" + id + ", " + si.getObjects().size() + ", '" + si.getDifficulty() + "');");
  91. System.out.println("Songinstance Added");
  92. }
  93. rs.close();
  94. st.close();
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. }
  100. else
  101. System.err.println("Insert Failed");
  102. }
  103. }
  104. private void executeInsertQuery(String query)
  105. {
  106. try{
  107. Statement s = myConn.createStatement();
  108. s.execute(query);
  109. s.close();
  110. }
  111. catch( SQLException ex)
  112. {
  113. System.out.println("SQLException: " + ex.getMessage());
  114. System.out.println("SQLState: " + ex.getSQLState());
  115. System.out.println("VendorError: " + ex.getErrorCode());
  116. }
  117. catch( Exception ex)
  118. {
  119. System.out.println("getMeasurement: " + ex.getMessage());
  120. }
  121. }
  122. //Execute a custom query
  123. private Statement executeResultQuery(String query)
  124. {
  125. Statement s = null;
  126. try{
  127. s = myConn.createStatement();
  128. s.executeQuery(query);
  129. }
  130. catch( SQLException ex)
  131. {
  132. System.out.println("SQLException: " + ex.getMessage());
  133. System.out.println("SQLState: " + ex.getSQLState());
  134. System.out.println("VendorError: " + ex.getErrorCode());
  135. }
  136. catch( Exception ex)
  137. {
  138. System.out.println("getMeasurement: " + ex.getMessage());
  139. }
  140. return s;
  141. }
  142. //Close connection with Database
  143. @Override
  144. public void finalize() throws Throwable
  145. {
  146. // Close database connection
  147. if( myConn != null )
  148. {
  149. try
  150. {
  151. myConn.close();
  152. System.out.println("Database connection terminated");
  153. }
  154. catch( Exception e ) {}
  155. }
  156. super.finalize();
  157. }
  158. }