Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package me.camdenorrb.ranks.MySql;
  2.  
  3. import java.io.PrintStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. public class MySql
  11. {
  12. private String host = "";
  13. private String database = "";
  14. private String user = "";
  15. private String password = "";
  16. private Connection con;
  17.  
  18. public MySql(String host, String database, String user, String password)
  19. {
  20. this.host = host;
  21. this.database = database;
  22. this.user = user;
  23. this.password = password;
  24. connect();
  25. }
  26.  
  27. public synchronized void connect()
  28. {
  29. try
  30. {
  31. this.con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":3306/" + this.database + "?autoReconnect=true", this.user, this.password);
  32.  
  33. System.out.println("[MySQL] The connection to MySQL is made!");
  34. }
  35. catch (SQLException e)
  36. {
  37. connect();
  38. }
  39. }
  40.  
  41. public synchronized void update(String qry)
  42. {
  43. try
  44. {
  45. PreparedStatement st = this.con.prepareStatement(qry);
  46. st.execute();
  47. st.close();
  48. }
  49. catch (Exception e)
  50. {
  51. connect();
  52. }
  53. }
  54.  
  55. public boolean hasConnection()
  56. {
  57. return this.con != null;
  58. }
  59.  
  60. public synchronized ResultSet query(String qry)
  61. {
  62. ResultSet rs = null;
  63. try
  64. {
  65. PreparedStatement st = this.con.prepareStatement(qry);
  66. rs = st.executeQuery();
  67. }
  68. catch (SQLException e)
  69. {
  70. connect();
  71. }
  72. return rs;
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement