Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. package de.letsplaybar.mysql;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import org.bukkit.configuration.file.FileConfiguration;
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13. import org.bukkit.entity.Player;
  14.  
  15. /**
  16. * @author Letsplaybar
  17. *
  18. */
  19.  
  20. public class MySQL {
  21.  
  22. private Main plugin;
  23. public Connection con;
  24. public String host;
  25. public int port;
  26. public String database;
  27. public String username;
  28. public String password;
  29.  
  30. public MySQL(Main main) {
  31. this.plugin = main;
  32.  
  33. File file =new File(plugin.getDataFolder(), "MySQL.yml");
  34. FileConfiguration sql = YamlConfiguration.loadConfiguration(file);
  35. String m = "MySQL.";
  36.  
  37.  
  38. sql.addDefault(m + "host", "localhost");
  39. sql.addDefault(m + "port", Integer.valueOf(3306));
  40. sql.addDefault(m + "database", "database");
  41. sql.addDefault(m + "username", "username");
  42. sql.addDefault(m + "password", "password");
  43. sql.options().copyDefaults(true);
  44. try {
  45. sql.save(file);
  46. } catch (IOException e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50.  
  51.  
  52.  
  53. host = sql.getString(m + "host");
  54. port = sql.getInt(m + "port");
  55. database = sql.getString(m + "database");
  56. username = sql.getString(m + "username");
  57. password = sql.getString(m + "password");
  58. //password ="";
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
  65. public void connect() {
  66. try{
  67. con = DriverManager.getConnection("jdbc:mysql://" + host + ":"+ port + "/" + database+ "?autoReconnect=true", username, password);
  68. }catch(Exception ex){
  69. ex.printStackTrace();
  70. }
  71.  
  72. }
  73.  
  74. public boolean isConected() {
  75. try {
  76. if(con.isClosed()|| con == null){
  77. return false;
  78. }
  79. } catch (SQLException e) {
  80. // TODO Auto-generated catch block
  81. e.printStackTrace();
  82. }
  83. return true;
  84. }
  85.  
  86. public void disconect() {
  87. if(con != null){
  88. try {
  89. con.close();
  90. } catch (SQLException ex) {
  91. ex.printStackTrace();
  92. }
  93. }
  94. }
  95.  
  96. public ResultSet query(String query) {
  97. if(!isConected()){
  98. connect();
  99. }
  100. ResultSet rs = null;
  101. try {
  102. Statement stm = con.createStatement();
  103. rs = stm.executeQuery(query);
  104. } catch (SQLException ex) {
  105. connect();
  106. ex.printStackTrace();
  107. }
  108. return rs;
  109. }
  110.  
  111. public void update(String query) {
  112. if(!isConected()){
  113. connect();
  114. }
  115. try {
  116. Statement stm = con.createStatement();
  117. System.out.println("Ausgefuehrtes Abfrage: "+query);
  118. stm.executeUpdate(query);
  119.  
  120. } catch (SQLException ex) {
  121. connect();
  122. ex.printStackTrace();
  123. }
  124. }
  125.  
  126.  
  127.  
  128. public boolean isInDatabase(Player target, String tabelle, String spalte) throws SQLException {
  129. String uuid = target.getUniqueId().toString();
  130.  
  131. boolean isInDatabase = false;
  132.  
  133. ResultSet rs = query("SELECT * FROM "+tabelle+" WHERE "+spalte+" = '" + uuid + "'");
  134.  
  135. if(rs.next()){
  136. isInDatabase = true;
  137. }else{
  138. isInDatabase = false;
  139.  
  140. }
  141.  
  142. return Boolean.valueOf(isInDatabase).booleanValue();
  143.  
  144.  
  145. }
  146.  
  147. public ResultSet getResult(String query){
  148. if(isConected()){
  149. try {
  150. return con.createStatement().executeQuery(query);
  151. } catch (SQLException e) {
  152. // TODO Auto-generated catch block
  153. e.printStackTrace();
  154. }
  155. }else{
  156. connect();
  157. getResult(query);
  158. }
  159.  
  160.  
  161. return null;
  162.  
  163. }
  164.  
  165. public ResultSet getPrepairedResult(String query){
  166. if(isConected()){
  167. try {
  168. return con.prepareStatement(query).executeQuery();
  169. } catch (SQLException e) {
  170. e.printStackTrace();
  171. }
  172. }else{
  173. connect();
  174. getPrepairedResult(query);
  175. }
  176. return null;
  177. }
  178.  
  179. public void createTableIfNotExist(String query){
  180. update("CREATE TABLE IF NOT EXIST "+query);
  181. }
  182.  
  183. public void createTable(String query){
  184. update("CREATE TABLE "+query);
  185. }
  186.  
  187. public void createDatabaseEintrag(String table, String spalten, String werte){
  188. update("INSERT INTO "+table+"("+spalten+") VALUES("+werte+")");
  189. }
  190.  
  191. public void setWertInTable(String table, String spalte, String wert, String fromSpalte, String wertFromSpalte){
  192. update("UPDATE "+table+" SET "+spalte+"='"+wert+"' WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  193. }
  194.  
  195. public void setWertInTable(String table, String spalte, int wert, String fromSpalte, String wertFromSpalte){
  196. update("UPDATE "+table+" SET "+spalte+"="+wert+" WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  197. }
  198.  
  199. public void setNULLInTable(String table, String spalte, String fromSpalte, String wertFromSpalte){
  200. update("UPDATE "+table+" SET "+spalte+"="+null+" WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  201. }
  202. public void setWertInTable(String table, String spalte, double wert, String fromSpalte, String wertFromSpalte){
  203. update("UPDATE "+table+" SET "+spalte+"='"+wert+"' WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  204. }
  205.  
  206. public ResultSet getWert(String table, String spalte, String fromSpalte){
  207. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ='"+fromSpalte+"'");
  208. }
  209.  
  210. public ResultSet getWert(String table, String spalte, int fromSpalte){
  211. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+fromSpalte);
  212. }
  213.  
  214. public ResultSet getWert(String table, String spalte, double fromSpalte){
  215. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+fromSpalte);
  216. }
  217. public ResultSet getWertfromNULL(String table, String spalte){
  218. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+null);
  219. }
  220.  
  221. public ResultSet sortierTable(String table, String bySpalte){
  222. return getPrepairedResult("SELECT * FROM "+table+" ORDER BY "+bySpalte+" DESC");
  223. }
  224.  
  225. public ResultSet giveTop3(String table, String bySpalte){
  226. return getPrepairedResult("SELECT * FROM "+table+" ORDER BY "+bySpalte+" DESC LIMIT 3");
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement