Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. /**
  2. *pour avoir les coins d'un joueur pas une uuid
  3. */
  4. public static int getCoins(UUID uuid) {
  5. int Coins = 0;
  6. ResultSet rs = db.getAllDataFromRow(db_money, "uuid", uuid.toString()).getResultSet();
  7. try {
  8. while(rs.next()) {
  9. Coins = rs.getInt("coins");
  10. }
  11. } catch(SQLException e) {
  12. e.printStackTrace();
  13. }
  14. return Coins;
  15. }
  16. //=============================================================================================================================
  17. //pour se co à la bdd
  18. MySQL.db.connect();
  19.  
  20. //=============================================================================================================================
  21. //MySQLManager
  22. public class MySQLManager
  23. {
  24. public static MySQLConnector getNewConnector(String ip, String port, String database, String username, String password)
  25. {
  26. return new MySQLConnector(ip, port, database, username, password);
  27. }
  28. }
  29. //=============================================================================================================================
  30. //MySQLResult
  31. import java.sql.ResultSet;
  32.  
  33. public class MySQLResult
  34. {
  35. private boolean success;
  36. private ResultSet result;
  37.  
  38. public MySQLResult(boolean success, ResultSet result)
  39. {
  40. this.success = success;
  41. this.result = result;
  42. }
  43.  
  44. public Boolean isSuccessful()
  45. {
  46. return Boolean.valueOf(this.success);
  47. }
  48.  
  49. public ResultSet getResultSet()
  50. {
  51. return this.result;
  52. }
  53. //=============================================================================================================================
  54. //MySQLconnector
  55. import java.sql.Connection;
  56. import java.sql.DriverManager;
  57. import java.sql.ResultSet;
  58. import java.sql.SQLException;
  59. import java.sql.Statement;
  60.  
  61. public class MySQLConnector {
  62.  
  63. private String ip;
  64. private String port;
  65. private String database;
  66. private String username;
  67. private String password;
  68. private Connection connection;
  69. private Statement statement;
  70. private boolean usable;
  71.  
  72. public MySQLConnector(String ip, String port, String database, String username, String password) {
  73. try {
  74. Class.forName("com.mysql.jdbc.Driver");
  75. this.ip = ip;
  76. this.port = port;
  77. this.database = database;
  78. this.username = username;
  79. this.password = password;
  80. this.usable = true;
  81. } catch (Exception ex) {
  82. this.usable = false;
  83. ex.printStackTrace();
  84. System.out.println(ex.getMessage());
  85. }
  86. }
  87.  
  88. public String getIP() {
  89. return this.ip;
  90. }
  91.  
  92. public String getPORT() {
  93. return this.port;
  94. }
  95.  
  96. public String getDatabaseName() {
  97. return this.database;
  98. }
  99.  
  100. public String getUsername() {
  101. return this.username;
  102. }
  103.  
  104. public String getPassword() {
  105. return this.password;
  106. }
  107.  
  108. public void connect() {
  109. try {
  110. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.port + "/" + this.database, this.username, this.password);
  111. this.statement = this.connection.createStatement();
  112. } catch (SQLException ex) {
  113. ex.printStackTrace();
  114. System.out.println(ex.getMessage());
  115. }
  116. }
  117.  
  118. public void disconnect() {
  119. try {
  120. this.connection.close();
  121. } catch (SQLException ex) {
  122. ex.printStackTrace();
  123. System.out.println(ex.getMessage());
  124. }
  125. }
  126.  
  127. public Boolean isConnected() {
  128. try {
  129. if (this.connection.isClosed()) {
  130. return Boolean.valueOf(false);
  131. }
  132. return Boolean.valueOf(true);
  133. } catch (Exception ex) {
  134. ex.printStackTrace();
  135. System.out.println(ex.getMessage());
  136. }
  137. return Boolean.valueOf(false);
  138. }
  139.  
  140. public Boolean isUsable() {
  141. return Boolean.valueOf(this.usable);
  142. }
  143.  
  144. public MySQLResult getAllDataFromTable(String tableName) {
  145. if(!MySQL.db.isConnected()) {
  146. MySQL.db.connect();
  147. }
  148. try {
  149. if (!this.usable) {
  150. return new MySQLResult(false, null);
  151. }
  152. ResultSet result = null;
  153. result = this.statement.executeQuery("SELECT * FROM " + tableName);
  154. return new MySQLResult(true, result);
  155. } catch (Exception ex) {
  156. ex.printStackTrace();
  157. System.out.println(ex.getMessage());
  158. }
  159. return new MySQLResult(false, null);
  160. }
  161.  
  162. public MySQLResult getAllDataFromRow(String tableName, String field, String value) {
  163. if(!MySQL.db.isConnected()) {
  164. MySQL.db.connect();
  165. }
  166. try {
  167. if (!this.usable) {
  168. return new MySQLResult(false, null);
  169. }
  170. ResultSet result = null;
  171. result = this.statement.executeQuery("SELECT * FROM " + tableName + " WHERE " + field + "='" + value + "'");
  172. return new MySQLResult(true, result);
  173. } catch (Exception ex) {
  174. ex.printStackTrace();
  175. System.out.println(ex.getMessage());
  176. }
  177. return new MySQLResult(false, null);
  178. }
  179.  
  180. public void createTable(String tableName, boolean MyISAM) {
  181. if(!MySQL.db.isConnected()) {
  182. MySQL.db.connect();
  183. }
  184. try {
  185. if (!this.usable) {
  186. return;
  187. }
  188. if (!MyISAM)
  189. this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName);
  190. else {
  191. this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName + " ENGINE=MyISAM");
  192. }
  193. } catch (Exception ex) {
  194. ex.printStackTrace();
  195. System.out.println(ex.getMessage());
  196. }
  197. }
  198.  
  199. public void removeTable(String tableName) {
  200. if(!MySQL.db.isConnected()) {
  201. MySQL.db.connect();
  202. }
  203. try {
  204. if (!this.usable) {
  205. return;
  206. }
  207. this.statement.execute("DROP TABLE IF EXISTS " + tableName);
  208. } catch (Exception ex) {
  209. ex.printStackTrace();
  210. System.out.println(ex.getMessage());
  211. }
  212. }
  213.  
  214. public void insertData(String tableName, String fields, String values) {
  215. if(!MySQL.db.isConnected()) {
  216. MySQL.db.connect();
  217. }
  218. try {
  219. if (!this.usable) {
  220. return;
  221. }
  222. this.statement.execute("INSERT INTO `" + tableName + "`(" + fields + ") VALUES (" + values + ")");
  223. } catch (Exception ex) {
  224. ex.printStackTrace();
  225. System.out.println(ex.getMessage());
  226. }
  227. }
  228.  
  229. public void updateAllData(String tableName, String field, String value) {
  230. if(!MySQL.db.isConnected()) {
  231. MySQL.db.connect();
  232. }
  233. try {
  234. if (!this.usable) {
  235. return;
  236. }
  237. this.statement.executeUpdate("UPDATE " + tableName + " SET " + field + "='" + value + "'");
  238. } catch (Exception ex) {
  239. ex.printStackTrace();
  240. System.out.println(ex.getMessage());
  241. }
  242. }
  243.  
  244. public void updateData(String tableName, String updateField, String updateValue, String field, String value) {
  245. if(!MySQL.db.isConnected()) {
  246. MySQL.db.connect();
  247. }
  248. try {
  249. if (!this.usable) {
  250. return;
  251. }
  252. this.statement.executeUpdate("UPDATE " + tableName + " SET " + updateField + "='" + updateValue + "' WHERE " + field + "='" + value + "'");
  253. } catch (Exception ex) {
  254. ex.printStackTrace();
  255. System.out.println(ex.getMessage());
  256. }
  257. }
  258.  
  259. public void deleteRow(String tableName, String field, String value) {
  260. if(!MySQL.db.isConnected()) {
  261. MySQL.db.connect();
  262. }
  263. try {
  264. if (!this.usable) {
  265. return;
  266. }
  267. this.statement.execute("DELETE FROM " + tableName + " WHERE " + field + "='" + value + "'");
  268. } catch (Exception ex) {
  269. ex.printStackTrace();
  270. System.out.println(ex.getMessage());
  271. }
  272.  
  273. }
  274.  
  275. public void clearTable(String tableName) {
  276. if(!MySQL.db.isConnected()) {
  277. MySQL.db.connect();
  278. }
  279. try {
  280. if (!this.usable) {
  281. return;
  282. }
  283. this.statement.execute("DELETE FROM " + tableName);
  284. } catch (Exception ex) {
  285. ex.printStackTrace();
  286. System.out.println(ex.getMessage());
  287. }
  288. }
  289.  
  290. public MySQLResult executeRawQuery(String rawSQL) {
  291. if(!MySQL.db.isConnected()) {
  292. MySQL.db.connect();
  293. }
  294. try {
  295. if (!this.usable) {
  296. return new MySQLResult(false, null);
  297. }
  298. return new MySQLResult(true, this.statement.executeQuery(rawSQL));
  299. } catch (Exception ex) {
  300. ex.printStackTrace();
  301. System.out.println(ex.getMessage());
  302. }
  303. return new MySQLResult(false, null);
  304. }
  305.  
  306. public void executeRaw(String rawSQL) {
  307. if(!MySQL.db.isConnected()) {
  308. MySQL.db.connect();
  309. }
  310. try {
  311. if (!this.usable) {
  312. return;
  313. }
  314. this.statement.execute(rawSQL);
  315. } catch (Exception ex) {
  316. ex.printStackTrace();
  317. System.out.println(ex.getMessage());
  318. }
  319. }
  320.  
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement