Advertisement
Guest User

Untitled

a guest
Apr 1st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. package me.tim.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class MySQL {
  10.  
  11.  
  12. private String user, password, host, database, port;
  13. private Connection con;
  14.  
  15. public Connection getCon() {
  16. return con;
  17. }
  18.  
  19. public void setCon(Connection con) {
  20. this.con = con;
  21. }
  22.  
  23. public MySQL(String user, String password, String host, String database, String port) {
  24.  
  25. this.user = user;
  26. this.password = password;
  27. this.host = host;
  28. this.database = database;
  29. this.port = port;
  30. }
  31.  
  32. public void connect() {
  33.  
  34. if(!isConnected()) {
  35. try {
  36.  
  37. setCon(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.user, this.password));
  38.  
  39. } catch (SQLException exc) {
  40. exc.printStackTrace();
  41. }
  42. }
  43. }
  44.  
  45. public boolean isConnected() {
  46. return getCon() !=null;
  47. }
  48.  
  49. public void close() {
  50. if(isConnected()) {
  51. try {
  52.  
  53. getCon().close();
  54.  
  55. } catch (SQLException exc) {
  56. exc.printStackTrace();
  57. }
  58. }
  59. }
  60.  
  61. public void update(String qry) {
  62. if(isConnected()) {
  63. try {
  64.  
  65. PreparedStatement ps = getCon().prepareStatement(qry);
  66. ps.executeUpdate();
  67.  
  68. } catch (SQLException exc) {
  69. exc.printStackTrace();
  70. }
  71. }
  72. }
  73.  
  74. public ResultSet query(String qry) {
  75. ResultSet rs = null;
  76. try {
  77.  
  78. PreparedStatement ps = getCon().prepareStatement(qry);
  79. rs = ps.executeQuery();
  80.  
  81. } catch (SQLException exc) {
  82.  
  83. }
  84.  
  85. return rs;
  86. }
  87.  
  88. public void createTable() {
  89. update("CREATE TABLE IF NOT EXISTS Stats(UUID varchar(64), Points int, Kills int, Deaths int");
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement