Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. package me.MrAxe.BeastTokens;
  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. private final String host, port, database, user, password;
  11.  
  12. private Connection con;
  13.  
  14. public MySQL(String host,String port, String database, String user, String password) {
  15.  
  16. this.host= host;
  17. this.port =port;
  18. this.database= database;
  19. this.user= user;
  20. this.password= password;
  21.  
  22. connect();
  23. }
  24.  
  25. public void connect() {
  26. try {
  27. con = DriverManager.getConnection("jdbc:mysql://" + host+ ":"+port+"/" + database+ "?autoReconnect=true&useSSL=false",
  28. user, password);
  29. System.out.println("[BeastTokens MySQL] The connection to MySQL is made!");
  30. } catch (SQLException e) {
  31. System.out.println("[BeastTokens MySQL] The connection to MySQL couldn't be made! reason: " + e.getMessage());
  32. }
  33. }
  34. public void close() {
  35. try {
  36. if (con != null) {
  37. con.close();
  38. System.out.println("[MySQL] The connection to MySQL is ended successfully!");
  39. }
  40. } catch (SQLException e) {
  41.  
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. public PreparedStatement prepareStatement(String qry) {
  47. PreparedStatement st = null;
  48. try {
  49. st = con.prepareStatement(qry);
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. return st;
  54. }
  55.  
  56. public void update(PreparedStatement statement) {
  57. try {
  58. statement.executeUpdate();
  59. } catch (SQLException e) {
  60. connect();
  61. e.printStackTrace();
  62. }finally{
  63. try {
  64. statement.close();
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. }
  68. }}
  69.  
  70. public boolean hasConnection() {
  71. return con != null;
  72. }
  73.  
  74. public ResultSet query(PreparedStatement statement) {
  75. try {
  76. return statement.executeQuery();
  77. } catch (SQLException e) {
  78. connect();
  79. e.printStackTrace();
  80. }
  81. return null;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement