2021-04-04 10:01:54 +02:00
|
|
|
/*
|
|
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
|
|
* To change this template file, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
package DataLayer;
|
|
|
|
|
2021-10-25 19:08:22 +02:00
|
|
|
import FunctionLayer.SimilarityMatrix;
|
|
|
|
import FunctionLayer.CustomError;
|
|
|
|
import com.google.common.collect.MapMaker;
|
2021-04-04 10:01:54 +02:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
2021-10-25 19:08:22 +02:00
|
|
|
import java.sql.Statement;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentMap;
|
2021-04-04 10:01:54 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
/**
|
2021-10-25 19:08:22 +02:00
|
|
|
*
|
2021-04-04 10:01:54 +02:00
|
|
|
* @author install1
|
|
|
|
*/
|
|
|
|
public class DataMapper {
|
|
|
|
|
2021-10-25 19:08:22 +02:00
|
|
|
public static void createTables() throws CustomError {
|
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
String l_sSQL = "CREATE TABLE IF NOT EXISTS `ArtificialAutism`.`Sentences` (`Strings` text NOT NULL)";
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
|
|
|
l_pStatement.execute();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
throw new CustomError("failed in DataMapper " + ex.getMessage());
|
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ConcurrentMap<Integer, String> getAllStrings() throws CustomError {
|
|
|
|
ConcurrentMap<Integer, String> allStrings = new MapMaker().concurrencyLevel(2).makeMap();
|
2021-04-04 10:01:54 +02:00
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
String l_sSQL = "SELECT * FROM `Sentences`";
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
|
|
|
l_rsSearch = l_pStatement.executeQuery();
|
2021-10-25 19:08:22 +02:00
|
|
|
int ij = 0;
|
2021-04-04 10:01:54 +02:00
|
|
|
while (l_rsSearch.next()) {
|
2021-10-25 19:08:22 +02:00
|
|
|
allStrings.put(ij, l_rsSearch.getString(1));
|
|
|
|
ij++;
|
2021-04-04 10:01:54 +02:00
|
|
|
}
|
2021-10-25 19:08:22 +02:00
|
|
|
} catch (SQLException ex) {
|
|
|
|
throw new CustomError("failed in DataMapper " + ex.getMessage());
|
2021-04-04 10:01:54 +02:00
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
2021-10-25 19:08:22 +02:00
|
|
|
return allStrings;
|
2021-04-04 10:01:54 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:08:22 +02:00
|
|
|
public static void InsertMYSQLStrings(ConcurrentMap<Integer, String> str) throws CustomError {
|
2021-04-04 10:01:54 +02:00
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
String l_sSQL = "INSERT IGNORE `Sentences` (`Strings`) VALUES (?)";
|
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
2021-10-25 19:08:22 +02:00
|
|
|
for (String str1 : str.values()) {
|
|
|
|
//System.out.println("adding str1: " + str1 + "\n");
|
2021-04-04 10:01:54 +02:00
|
|
|
l_pStatement.setString(1, str1);
|
2021-10-25 19:08:22 +02:00
|
|
|
l_pStatement.addBatch();
|
2021-04-04 10:01:54 +02:00
|
|
|
}
|
2021-10-25 19:08:22 +02:00
|
|
|
l_pStatement.executeBatch();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
throw new CustomError("failed in DataMapper " + ex.getMessage());
|
2021-04-04 10:01:54 +02:00
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:08:22 +02:00
|
|
|
public static ConcurrentMap<Integer, String> getHLstatsMessages() {
|
|
|
|
ConcurrentMap<Integer, String> hlStatsMessages = new MapMaker().concurrencyLevel(2).makeMap();
|
|
|
|
try (Connection l_cCon = DBCPDataSourceHLstats.getConnection()) {
|
|
|
|
String l_sSQL = "SELECT message FROM `hlstats_Events_Chat`";
|
|
|
|
try (PreparedStatement l_pStatement = l_cCon.prepareStatement(l_sSQL)) {
|
|
|
|
try (ResultSet l_rsSearch = l_pStatement.executeQuery()) {
|
|
|
|
while (l_rsSearch.next()) {
|
|
|
|
hlStatsMessages.put(hlStatsMessages.size() + 1, l_rsSearch.getString(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
return hlStatsMessages;
|
|
|
|
}
|
2021-04-04 10:01:54 +02:00
|
|
|
|
|
|
|
public static void CloseConnections(PreparedStatement ps, ResultSet rs, Connection con) {
|
|
|
|
if (rs != null) {
|
|
|
|
try {
|
|
|
|
rs.close();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ps != null) {
|
|
|
|
try {
|
|
|
|
ps.close();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (con != null) {
|
|
|
|
try {
|
|
|
|
con.close();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|