Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. package tequilaxbr.dixeelban.utils;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. public class SQL {
  12.  
  13. private String user, host, database, password;
  14. private Connection connection;
  15. private Statement statement;
  16. private SQLType sqlType;
  17. private File db;
  18.  
  19. public SQL(String user, String password, String host, String database, SQLType sqlType) {
  20. this.user = user;
  21. this.password = password;
  22. this.host = host;
  23. this.database = database;
  24. this.sqlType = sqlType;
  25. }
  26.  
  27. public SQL(String database, File folder, SQLType sqlType){
  28. this.db = folder;
  29. this.database = database;
  30. this.sqlType = sqlType;
  31. }
  32.  
  33. public void startConnection(){
  34. if(getType()){
  35. try {
  36. Class.forName("com.mysql.jdbc.Driver");
  37. this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database + "", user, password);
  38. this.statement = this.connection.createStatement();
  39. this.statement.execute("CREATE TABLE IF NOT EXISTS bans (nickname VARCHAR(255), tempo LONG NOT NULL, banid INT NOT NULL)");
  40. } catch (SQLException | ClassNotFoundException e) {
  41. e.printStackTrace();
  42. }
  43. }else{
  44. try {
  45. Class.forName("org.sqlite.JDBC");
  46. this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.db.getAbsolutePath() + File.separator + database+".db");
  47. this.statement = this.connection.createStatement();
  48. this.statement.execute("CREATE TABLE IF NOT EXISTS bans (nickname VARCHAR(255), tempo LONG NOT NULL, banid INT NOT NULL)");
  49. } catch (ClassNotFoundException | SQLException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54.  
  55. public void closeConnection(){
  56. try {
  57. this.statement.close();
  58. this.connection.close();
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public void addBanPlayer(String id, int minutes, int banid){
  65. if(getType()){
  66. if(!hasBanPlayer(id)){
  67. try {
  68. Class.forName("com.mysql.jdbc.Driver");
  69. long timerAll = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(minutes);
  70. this.statement.executeUpdate("INSERT INTO bans (nickname, tempo, banid) VALUES ('"+ id + "','" + timerAll + "','" + banid + "')");
  71. } catch (ClassNotFoundException | SQLException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }else{
  76. if(!hasBanPlayer(id)){
  77. try {
  78. Class.forName("org.sqlite.JDBC");
  79. long timerAll = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(minutes);
  80. this.statement.executeUpdate("INSERT INTO bans (nickname, tempo, banid) VALUES ('"+ id + "','" + timerAll + "','" + banid + "')");
  81. } catch (ClassNotFoundException | SQLException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. }
  87.  
  88. public long getBanTimer(String id){
  89. if(getType()){
  90. try {
  91. Class.forName("com.mysql.jdbc.Driver");
  92. ResultSet rs = this.statement.executeQuery("SELECT tempo FROM bans WHERE nickname='" + id + "'");
  93. while(rs.next()){
  94. return rs.getLong("tempo");
  95. }
  96. } catch (SQLException | ClassNotFoundException e) {
  97. e.printStackTrace();
  98. }
  99. }else{
  100. try {
  101. Class.forName("org.sqlite.JDBC");
  102. ResultSet rs = this.statement.executeQuery("SELECT tempo FROM bans WHERE nickname='" + id + "'");
  103. while(rs.next()){
  104. return rs.getLong("tempo");
  105. }
  106. } catch (ClassNotFoundException | SQLException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. return 0;
  111. }
  112.  
  113. public boolean hasBanPlayer(String id){
  114. if(getType()){
  115. try {
  116. Class.forName("com.mysql.jdbc.Driver");
  117. ResultSet rs = this.statement.executeQuery("SELECT * FROM bans WHERE nickname='"+id+"'");
  118. while(rs.next()){
  119. return true;
  120. }
  121. return false;
  122. } catch (SQLException | ClassNotFoundException e) {
  123. }
  124.  
  125. }else{
  126. try {
  127. Class.forName("org.sqlite.JDBC");
  128. ResultSet rs = this.statement.executeQuery("SELECT * FROM bans WHERE nickname='"+id+"'");
  129. while(rs.next()){
  130. return true;
  131. }
  132. return false;
  133. } catch (SQLException | ClassNotFoundException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. return false;
  138. }
  139.  
  140. public boolean getType(){
  141. if(sqlType == SQLType.MySQL){
  142. return true;
  143. }else{
  144. return false;
  145. }
  146. }
  147.  
  148. public enum SQLType{
  149. MySQL, SQLite;
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement