package de.mrbesen.youtubecrawler; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; import org.apache.log4j.Logger; import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation; import de.mrbesen.youtubecrawler.Crawler.Video; public class DB implements Runnable { private Connection con; private String server = Config.prop.getProperty("db.host", "localhost"), user = Config.prop.getProperty("db.user", "ytcrawler"), pw = Config.prop.getProperty("db.pw", ""), db = Config.prop.getProperty("db.dbname", "ytcrawler"); private int port = Integer.parseInt(Config.prop.getProperty("db.port", "3306")); private Logger log = Logger.getLogger(DB.class.getName()); private ArrayList randombuffer = new ArrayList<>(100); private Random rand = new Random(); private Server serv = new Server(this); private Thread randomrefill = null; public DB() { try { connect(false); //set the database up! boolean found = false; ResultSet set = con.getMetaData().getCatalogs();//does the db exists? while(set.next()) { if(set.getString(1).equalsIgnoreCase(db)) { found = true; con.setCatalog(db); break; } } if(!found) {//DataBase not found, try to create log.warn("Database not found! tring to create!"); //create DB, table: konten / player / Transactions update("CREATE DATABASE `" + db + "` /*!40100 DEFAULT CHARACTER SET latin1*/;"); con.setCatalog(db); update("CREATE TABLE `videos` (`id` varchar(13) NOT NULL,`length` int(11) NOT NULL,`created` int(11) NOT NULL,`langcode` varchar(3) NOT NULL DEFAULT 'en',`category` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_UNIQUE` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"); update("CREATE TABLE `temp` ( `ytid` varchar(13) NOT NULL COMMENT 'a Table to store Video ids, when they are found to process them later', PRIMARY KEY (`ytid`), UNIQUE KEY `ytid_UNIQUE` (`ytid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); log.info("Database is set up!"); } serv.start(); refillbuffer(); } catch (SQLException e) { log.error("Error while connecting to the database! ", e); } } private void connect(boolean selectdb) { try { Class.forName("com.mysql.jdbc.Driver");//Treiber laden try this driver: com.mysql.cj.jdbc.Driver //verbinden con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + (selectdb ? db : "") + "?serverTimezone=UTC" ,user,pw); }catch (ClassNotFoundException | SQLException e) { log.error("Error while connecting to the database! ", e); } } /** * removes all videos, that are known from the db * @param input * @return */ public List checkvideos(List input) { if(!input.isEmpty()) { StringBuilder ids = new StringBuilder(); for(int i = 0; i < input.size(); i++) { ids.append(',').append(input.get(i)); } String query = "SELECT `id` FROM `videos` WHERE concat('%',`id`,'%') LIKE '" + ids.toString() + "';"; ResultSet res = query(query); try { while(res.next()) { input.remove(res.getString(1)); } } catch(SQLException e) { e.printStackTrace(); } } return input; } /** * save the list of videos to the DB * @param input */ public void addVideos(List