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:27:47 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
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:27:47 +02:00
|
|
|
import java.util.*;
|
2021-04-04 10:01:54 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author install1
|
|
|
|
*/
|
|
|
|
public class DataMapper {
|
|
|
|
|
2021-12-03 21:06:10 +01:00
|
|
|
public static ArrayList<String> getAllStrings() {
|
2021-04-04 10:01:54 +02:00
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
2021-10-25 19:27:47 +02:00
|
|
|
ArrayList<String> arrayListStr = new ArrayList();
|
2021-04-04 10:01:54 +02:00
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
2021-12-02 01:41:06 +01:00
|
|
|
String l_sSQL = "SELECT * FROM `Sentences` order by LENGTH(Strings) desc";
|
2021-04-04 10:01:54 +02:00
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
|
|
|
l_rsSearch = l_pStatement.executeQuery();
|
|
|
|
while (l_rsSearch.next()) {
|
2021-10-25 19:27:47 +02:00
|
|
|
arrayListStr.add(l_rsSearch.getString(1));
|
2021-04-04 10:01:54 +02:00
|
|
|
}
|
2021-12-03 21:06:10 +01:00
|
|
|
} catch (SQLException throwables) {
|
|
|
|
throwables.printStackTrace();
|
2021-04-04 10:01:54 +02:00
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
2021-10-25 19:27:47 +02:00
|
|
|
return arrayListStr;
|
2021-04-04 10:01:54 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 21:06:10 +01:00
|
|
|
public static void InsertMYSQLStrings(String str) {
|
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-12-01 23:45:36 +01:00
|
|
|
l_pStatement.setString(1, str);
|
|
|
|
l_pStatement.execute();
|
2021-12-03 21:06:10 +01:00
|
|
|
} catch (SQLException throwables) {
|
|
|
|
throwables.printStackTrace();
|
2021-04-04 10:01:54 +02:00
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 19:27:47 +02:00
|
|
|
|
|
|
|
public static void checkStringsToDelete() {
|
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
2021-11-29 19:37:52 +01:00
|
|
|
String CountSQL = "select count(*) from Sentences";
|
2021-10-25 19:27:47 +02:00
|
|
|
String l_sSQL = "delete from Sentences\n" +
|
2021-12-07 22:26:19 +01:00
|
|
|
" where DATE(last_used) < DATE_SUB(CURDATE(), INTERVAL 10 DAY)\n" +
|
2021-12-01 23:45:36 +01:00
|
|
|
" order by last_used asc limit 2";
|
2021-10-25 19:27:47 +02:00
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
2021-11-29 19:37:52 +01:00
|
|
|
l_pStatement = l_cCon.prepareStatement(CountSQL);
|
|
|
|
ResultSet resultSet = l_pStatement.executeQuery();
|
|
|
|
if (resultSet.next()) {
|
|
|
|
int count = resultSet.getInt(1);
|
|
|
|
if (count > 8500) {
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
|
|
|
l_pStatement.execute();
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 19:27:47 +02:00
|
|
|
} catch (SQLException throwables) {
|
|
|
|
throwables.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateLastUsed(@NotNull ArrayList<String> mysqlUpdateLastUsed) {
|
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
String l_sSQL = "update Sentences Set last_used = now() where Strings = (?)";
|
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
|
|
|
for (String str1 : mysqlUpdateLastUsed) {
|
|
|
|
l_pStatement.setString(1, str1);
|
|
|
|
l_pStatement.execute();
|
|
|
|
}
|
|
|
|
} catch (SQLException throwables) {
|
|
|
|
throwables.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 10:01:54 +02:00
|
|
|
}
|