Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. private String HOST = "";
  4. private String DATABASE = "";
  5. private String PASSWORD = "";
  6. private String USER = "";
  7.  
  8. private Connection con;
  9.  
  10. public MySQL(String host, String databse, String user, String password) {
  11. this.HOST = host;
  12. this.DATABASE = databse;
  13. this.USER = user;
  14. this.PASSWORD = password;
  15.  
  16. connect();
  17. }
  18.  
  19. private void connect() {
  20. try{
  21. con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/"+ DATABASE + "?autoReconnect=true", USER , PASSWORD);
  22. System.out.println("[ScrCloud] MySQL Connected !");
  23. }catch (SQLException e) {
  24. System.out.println("[ScrCloud] SystemError , ErrorCode : "+e.getMessage());
  25. e.printStackTrace();
  26. }
  27.  
  28. }
  29. public void close() {
  30. try {
  31. if(con != null) {
  32. con.close();
  33. System.out.println("[ScrCloud] Conection to MySQL is offline ");
  34. }
  35. } catch (SQLException e) {
  36. System.out.println("[ScrCloud] SystemError , ErrorCode : "+e.getMessage());
  37. e.printStackTrace();
  38. }
  39. }
  40.  
  41. public void update(String qry) {
  42. try {
  43. Statement st = con.createStatement();
  44. st.executeUpdate(qry);
  45. st.close();
  46. } catch (SQLException e) {
  47. connect();
  48. System.err.println(e);
  49. }
  50.  
  51. }
  52. public ResultSet query(String qry) {
  53. ResultSet rs = null;
  54. try {
  55. Statement st = con.createStatement();
  56. rs = st.executeQuery(qry);
  57. } catch (SQLException e) {
  58.  
  59. e.printStackTrace();
  60. }
  61. return rs;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement