Advertisement
Guest User

Untitled

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