diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..6462638 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://10.69.0.2:3306/buzzbot + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..005d496 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 45b67fd..9551fbc 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,7 @@ dependencies { implementation group: 'me.duncte123', name: 'botCommons', version: '2.3.8' implementation 'com.google.code.gson:gson:2.9.0' implementation 'me.carleslc.Simple-YAML:Simple-Yaml:1.7.2' + implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.28' } compileJava.options.encoding = 'UTF-8' \ No newline at end of file diff --git a/src/main/java/net/nevet5/buzzbot/Config.java b/src/main/java/net/nevet5/buzzbot/Config.java index fbe97c3..a30b4e6 100644 --- a/src/main/java/net/nevet5/buzzbot/Config.java +++ b/src/main/java/net/nevet5/buzzbot/Config.java @@ -3,6 +3,8 @@ package net.nevet5.buzzbot; import org.simpleyaml.configuration.file.YamlFile; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; public class Config { private static final YamlFile YML_FILE = new YamlFile("./config.yml"); @@ -14,10 +16,6 @@ public class Config { System.out.println("Config file not found, creating new one..."); YML_FILE.createNewFile(true); System.out.println("Config file created!"); -// YML_FILE.addDefault("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); -// YML_FILE.addDefault("bot.prefix", "!"); -// YML_FILE.set("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); -// YML_FILE.set("bot.prefix", "!"); } else { System.out.println("Loading Config file..."); YML_FILE.loadWithComments(); @@ -27,11 +25,16 @@ public class Config { System.out.println("Error while loading config file!"); e.printStackTrace(); } + YML_FILE.addDefault("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); YML_FILE.addDefault("bot.prefix", "!"); YML_FILE.addDefault("bot.activity", "playing"); YML_FILE.addDefault("bot.action", "with myself"); + YML_FILE.addDefault("database.url", "localhost"); + YML_FILE.addDefault("database.user", "barry"); + YML_FILE.addDefault("database.password", "benson"); + YML_FILE.addDefault("database.database", "buzzbot"); try { YML_FILE.save(); diff --git a/src/main/java/net/nevet5/buzzbot/Test.java b/src/main/java/net/nevet5/buzzbot/Test.java new file mode 100644 index 0000000..ec29170 --- /dev/null +++ b/src/main/java/net/nevet5/buzzbot/Test.java @@ -0,0 +1,9 @@ +package net.nevet5.buzzbot; + +import java.time.LocalTime; + +public class Test { + public static void main(String[] args) { + + } +} diff --git a/src/main/java/net/nevet5/buzzbot/database/SqlDB.java b/src/main/java/net/nevet5/buzzbot/database/SqlDB.java new file mode 100644 index 0000000..be5afef --- /dev/null +++ b/src/main/java/net/nevet5/buzzbot/database/SqlDB.java @@ -0,0 +1,188 @@ +package net.nevet5.buzzbot.database; + +import net.nevet5.buzzbot.Config; + +import java.sql.*; +import java.time.LocalDate; +import java.time.LocalTime; + +public class SqlDB { + //TODO Make this a two way class for reading and writing from db + + private int userId; + private String userName; + private int userDiscriminator; + private String date = LocalDate.now().toString(); + private String time = LocalTime.now().toString().split("\\.")[0]; + private boolean banType; + private int banLength; + private String banReason; + private String modName; + private int modId; + private String serverName; + private int serverId; + + private Connection connect; + private Statement statement; + private PreparedStatement preparedStatement; + private ResultSet resultSet; + + public SqlDB() { + try { + Class.forName("com.mysql.jdbc.Driver"); + connect = DriverManager.getConnection("jdbc:mysql://" + Config.getConfig().getString("database.url") + "/" + Config.getConfig().getString("database.database") + "?user=" + Config.getConfig().getString("database.user") + "&password=" + Config.getConfig().getString("database.password")); + statement = connect.createStatement(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void readDatabase() { + try { + Class.forName("com.mysql.jdbc.Driver"); + + connect = DriverManager.getConnection("jdbc:mysql://" + Config.getConfig().getString("database.url") + "/" + Config.getConfig().getString("database.database") + "?user=" + Config.getConfig().getString("database.user") + "&password=" + Config.getConfig().getString("database.password")); + + statement = connect.createStatement(); + + resultSet = statement + .executeQuery("SELECT * FROM masterbanlist"); + } catch (ClassNotFoundException | SQLException e) { + throw new RuntimeException(e); + } + } + + public void insert() { + try { + statement = connect.createStatement(); + + resultSet = statement.executeQuery("INSERT INTO " + Config.getConfig().getString("database.database") + " VALUES (default, " + userId + ", '" + userName + "', '" + userDiscriminator + "', '" + date + "', '" + time + "', " + banType + ", " + banLength + ", '" + banReason + "', '" + modName + "', " + modId + ", '" + serverName + "', " + serverId + ");"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + private void writeResultSet(ResultSet resultSet) throws SQLException { + while (resultSet.next()) { + userId = resultSet.getInt("userid"); + userName = resultSet.getString("username"); + userDiscriminator = resultSet.getInt("userdiscriminator"); +// date; +// time; +// banType; +// banLength; +// banReason; +// modName; +// modId; +// serverName; +// serverId; + + String user = resultSet.getString("myuser"); + String website = resultSet.getString("webpage"); + String summary = resultSet.getString("summary"); + Date date = resultSet.getDate("datum"); + String comment = resultSet.getString("comments"); + System.out.println("User: " + user); + System.out.println("Website: " + website); + System.out.println("summary: " + summary); + System.out.println("Date: " + date); + System.out.println("Comment: " + comment); + } + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public int getUserId() { + return userId; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserName() { + return userName; + } + + public void setUserDiscriminator(int userDiscriminator) { + this.userDiscriminator = userDiscriminator; + } + + public int getUserDiscriminator() { + return userDiscriminator; + } + + public void setDate(String date) { + this.date = date; + } + + public String getDate() { + return date; + } + + public void setTime(String time) { + this.time = time; + } + + public String getTime() { + return time; + } + + public void setBanType(boolean banType) { + this.banType = banType; + } + + public boolean getBanType() { + return banType; + } + + public void setBanLength(int banLength) { + this.banLength = banLength; + } + + public int getBanLength() { + return banLength; + } + + public void setBanReason(String banReason) { + this.banReason = banReason; + } + + public String getBanReason() { + return banReason; + } + + public void setModName(String modName) { + this.modName = modName; + } + + public String getModName() { + return modName; + } + + public void setModId(int modId) { + this.modId = modId; + } + + public int getModId() { + return modId; + } + + public void setServerName(String serverName) { + this.serverName = serverName; + } + + public String getServerName() { + return serverName; + } + + public void setServerId(int serverId) { + this.serverId = serverId; + } + + public int getServerId() { + return serverId; + } +}