package src;

import java.sql.*;

public class DataBaseWorker {

        private String _dbName;
        private String _tableName;
        private String _password;
        private String _userName;
        private Connection _con;
        private Statement _statement;

        DataBaseWorker(String dbName, String tableName, String userName, String password)
        {
            _tableName = tableName;
            _password = password;
            _userName = userName;
            _dbName = dbName;

            try {
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (IllegalAccessException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                String url = "jdbc:mysql://localhost/" + _dbName;
                String name = userName;
                String accessPassword = password;
                try {
                    _con = DriverManager.getConnection(url, name, accessPassword);
                    _statement = _con.createStatement();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }

        public void InsertInTable(String firstPlayer, String firstPlayerLanguage, String secondPlayer, String secondPlayerLanguage, int gameResult) throws SQLException {

            String sql = "DELETE FROM `" + _tableName + "` WHERE player1 = '" + firstPlayer + "' AND lang1 = '" + firstPlayerLanguage + "' AND player2 = '" + secondPlayer + "' AND lang2 = '" + secondPlayerLanguage + "'";
            _statement.execute("DELETE FROM `" + _tableName + "` WHERE player1 = '" + firstPlayer + "' AND lang1 = '" + firstPlayerLanguage + "' AND player2 = '" + secondPlayer + "' AND lang2 = '" + secondPlayerLanguage + "'");
            _statement.execute("DELETE FROM `" + _tableName + "` WHERE player1 = '" + secondPlayer + "' AND lang1 = '" + secondPlayerLanguage + "' AND player2 = '" + firstPlayer + "' AND lang2 = '" + firstPlayerLanguage + "'");

            _statement.execute("INSERT INTO `" + _tableName + "` VALUES ('" + secondPlayer + "', '" + secondPlayerLanguage + "', '" + firstPlayer + "', '" + firstPlayerLanguage + "', " + gameResult + ")");
            _statement.execute("INSERT INTO `" + _tableName + "` VALUES ('" + firstPlayer + "', '" + firstPlayerLanguage + "', '" + secondPlayer + "', '" + secondPlayerLanguage + "', " + gameResult * (-1) + ")");
        }

        protected void finalize() throws Throwable {
        _con.close();
    }

}

@@ -1676,229 +1676,8 @@
  %7B%0A%0A
-            String sql = %22DELETE FROM %60%22 + _tableName + %22%60 WHERE player1 = '%22 + firstPlayer + %22' AND lang1 = '%22 + firstPlayerLanguage + %22' AND player2 = '%22 + secondPlayer + %22' AND lang2 = '%22 + secondPlayerLanguage + %22'%22;%0A
     
@@ -1735,39 +1735,38 @@
 Name + %22%60 WHERE 
-p
+P
 layer
-1
  = '%22 + firstPla
@@ -1770,37 +1770,42 @@
 Player + %22' AND 
+P
 la
+yerLa
 ng
-1
  = '%22 + firstPla
@@ -1817,39 +1817,40 @@
 nguage + %22' AND 
-player2
+Opponent
  = '%22 + secondPl
@@ -1855,37 +1855,44 @@
 Player + %22' AND 
-l
+OpponentL
 ang
-2
  = '%22 + secondPl
@@ -1987,15 +1987,14 @@
 ERE 
-p
+P
 layer
-1
  = '
@@ -2023,13 +2023,18 @@
 AND 
+P
 la
+yerLa
 ng
-1
  = '
@@ -2071,15 +2071,16 @@
 AND 
-player2
+Opponent
  = '
@@ -2108,13 +2108,20 @@
 AND 
-l
+OpponentL
 ang
-2
  = '
@@ -2352,16 +2352,29 @@
 Result +
+ %22, 'ver1'%22 +
  %22)%22);%0A 
@@ -2578,16 +2578,29 @@
 * (-1) +
+ %22, 'ver1'%22 +
  %22)%22);%0A 

Rewrite DataBaseWorker: adaptate with new DB