| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package data.io;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.List;
- import model.objects.Highscore;
- import audio.Song;
- import audio.SongInstance;
- public class SQLConnector
- {
- private Connection myConn = null;
-
- public SQLConnector()
- {
- this("178.62.254.153", "3306","colorstrike","csadmin","aardbei123");
- }
- //Start connection with Databse
- public SQLConnector(String host, String port, String dbName, String userName, String password)
- {
- try
- {
- String url = "jdbc:mysql://" + host + ":" + port + "/"+ dbName + "?user="
- + userName
- + "&password="
- + password;
- Class.forName("com.mysql.jdbc.Driver").newInstance ();
- myConn = DriverManager.getConnection(url);
- }
- catch( SQLException ex)
- {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- catch(Exception ex)
- {
- System.out.println("Error : " + ex.getMessage());
- }
- }
-
- public List<Highscore> getHighscore(String song)
- {
- Statement st = executeResultQuery("SELECT * FROM highscore");
- ResultSet result = null;
- try {
- result = st.getResultSet();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- while(result.next())
- {
-
- }
- } catch (SQLException e) {
- }
-
- return null;
- }
-
- public void update(List<Song> songs)
- {
- for(Song s : songs)
- {
- executeInsertQuery("INSERT INTO song (folder, title, author) VALUES ('" + s.getFolder() + "', '" + s.getTitle() + "', '" + s.getAuthor() + "');");
-
- Statement st = executeResultQuery("SELECT id FROM song WHERE folder='" + s.getFolder() + "'");
-
- ResultSet rs = null;
- int id = -1;
-
- try {
- rs = st.getResultSet();
- rs.next();
- id = rs.getInt(1);
- System.out.println("Song " + id + " added to database");
- rs.close();
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
-
- if(id != -1)
- {
- for(SongInstance si : s.getSongs())
- {
- Statement st2 = executeResultQuery("SELECT id FROM songinstance WHERE song=" + id + " AND difficulty='" + si.getDifficulty() + "'");
-
- ResultSet rs2 = null;
-
- try {
- rs2 = st2.getResultSet();
- if(rs2.first())
- {
- System.err.println("SongInstance Already Exists ");
- }
- else
- {
- executeInsertQuery("INSERT INTO songinstance (song, enemies, difficulty) VALUES (" + id + ", " + si.getObjects().size() + ", '" + si.getDifficulty() + "');");
- System.out.println("Songinstance Added");
- }
- rs.close();
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- else
- System.err.println("Insert Failed");
-
- }
- }
-
- private void executeInsertQuery(String query)
- {
- try{
- Statement s = myConn.createStatement();
- s.execute(query);
- s.close();
- }
- catch( SQLException ex)
- {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- catch( Exception ex)
- {
- System.out.println("getMeasurement: " + ex.getMessage());
- }
- }
- //Execute a custom query
- private Statement executeResultQuery(String query)
- {
- Statement s = null;
-
- try{
- s = myConn.createStatement();
- s.executeQuery(query);
-
- }
- catch( SQLException ex)
- {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- catch( Exception ex)
- {
- System.out.println("getMeasurement: " + ex.getMessage());
- }
-
- return s;
- }
-
- //Close connection with Database
- @Override
- public void finalize() throws Throwable
- {
- // Close database connection
- if( myConn != null )
- {
- try
- {
- myConn.close();
- System.out.println("Database connection terminated");
- }
- catch( Exception e ) {}
- }
- super.finalize();
- }
- }
|