Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package me.joseph.murder;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class MySQL {
  10.  
  11. private String host;
  12. private String database;
  13. private String user;
  14. private String password;
  15. private String port;
  16. private Connection con;
  17.  
  18. public MySQL(String host, String port, String database, String user, String password) {
  19. this.host = host;
  20. this.database = database;
  21. this.user = user;
  22. this.password = password;
  23. this.port = port;
  24.  
  25. connect();
  26.  
  27. if (isConnected()) {
  28. System.out.println("[Murder] Connected to MySQL database.");
  29. createTable();
  30. }
  31. if (!isConnected()) {
  32. System.out.println("[Murder] Could not connect to MySQL.");
  33. }
  34. }
  35.  
  36. public boolean isConnected() {
  37. try {
  38. if (getConnection().isClosed()) {
  39. return false;
  40. }
  41. return true;
  42. } catch (Exception e) {
  43. }
  44. return false;
  45. }
  46.  
  47. public void createTable() {
  48.  
  49. try (Statement statement = getConnection().createStatement();) {
  50. statement.executeUpdate(
  51. "CREATE TABLE IF NOT EXISTS MurderData (uuid VARCHAR(64), wins INT(10), loses INT(10), deaths INT(10), kills INT(10), score INT(10));");
  52.  
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57.  
  58. public void connect() {
  59. try {
  60. con = DriverManager.getConnection(
  61. "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true",
  62. this.user, this.password);
  63.  
  64. } catch (SQLException ex) {
  65. ex.printStackTrace();
  66.  
  67. }
  68. }
  69.  
  70. public void close() {
  71. if (isConnected()) {
  72. try {
  73. if (con != null) {
  74. con.close();
  75. }
  76. } catch (SQLException ex) {
  77. ex.printStackTrace();
  78. }
  79. }
  80. }
  81.  
  82. public ResultSet query(String qry) {
  83. if (isConnected()) {
  84. ResultSet rs = null;
  85.  
  86. try {
  87. Statement st = con.createStatement();
  88. rs = st.executeQuery(qry);
  89. } catch (SQLException ex) {
  90. connect();
  91. ex.printStackTrace();
  92. }
  93.  
  94. return rs;
  95. }
  96. return null;
  97. }
  98.  
  99. public void update(String qry) {
  100. if (isConnected()) {
  101. try {
  102. Statement st = con.createStatement();
  103. st.executeUpdate(qry);
  104. st.close();
  105. } catch (SQLException ex) {
  106. connect();
  107. ex.printStackTrace();
  108. }
  109. }
  110. }
  111.  
  112. public Connection getConnection() {
  113. return con;
  114.  
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement