· 5 years ago · Mar 18, 2020, 06:02 PM
1package de.progen_bot.db.dao.autorole;
2
3import de.progen_bot.db.connection.ConnectionFactory;
4import de.progen_bot.db.dao.Dao;
5import net.dv8tion.jda.api.entities.Guild;
6
7import javax.management.relation.Role;
8import java.sql.Connection;
9import java.sql.PreparedStatement;
10import java.sql.ResultSet;
11import java.sql.SQLException;
12import java.util.ArrayList;
13import java.util.List;
14import java.util.SimpleTimeZone;
15
16public class AutoroleDaoImpl extends Dao implements AutoroleDao {
17 final private String sqlQuery = "CREATE TABLE IF NOT EXISTS autorole(`id` INT(11) NOT NULL AUTO_INCREMENT, ´guildid´" + "VARCHAR(50) NOT NULL, `roleid`VARCHAR(50) NOT NULL" +
18 "PRIMARY KEY(`id`)) ENGINE = InnoDB DEFAULT CHARSET = utf8";
19
20 @Override
21 public void insertautorole(Role role, Guild guild) {
22 Connection connection = ConnectionFactory.getConnection();
23 try{
24 PreparedStatement ps = connection.prepareStatement("INSERT INTO `autorole` (guildid, role) VALUES (?,?,?)");
25 ps.setString(1, role.getRoleName());
26 ps.setString(2, guild.getId());
27 ps.execute();
28 } catch (SQLException e) {
29 e.printStackTrace();
30 }
31 }
32
33 @Override
34 public List<String> loadautorole(Role role, Guild guild) {
35 List<String> roles = new ArrayList<>();
36 Connection connection = ConnectionFactory.getConnection();
37 try {
38 PreparedStatement ps = connection.prepareStatement("SELECT * FROM `autorole` WHERE `guildid` =? and `roleid` =?");
39 ps.setString(1, role.getRoleName());
40 ps.setString(2, guild.getId());
41
42 ResultSet rs = ps.executeQuery();
43
44 while (rs.next()){
45 roles.add(rs.getString("autorole"));
46 }
47 } catch (SQLException e) {
48 e.printStackTrace();
49 }
50 return null;
51 }
52
53 @Override
54 public void deleteautorole(Role role, Guild guild) {
55 Connection connection = ConnectionFactory.getConnection();
56 try {
57 PreparedStatement ps = connection.prepareStatement("DELETE FROM `autorole` WHERE `guildid` = ? AND `roleid` = ?");
58 ps.setString(1, role.getRoleName());
59 ps.setString(2, guild.getId());
60 ps.execute();
61 } catch (SQLException e) {
62 e.printStackTrace();
63 }
64 }
65}