Advertisement
Guest User

MySQL

a guest
May 17th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. private String host = "";
  2. private String dataBase = "";
  3. private String user = "";
  4. private String password = "";
  5.  
  6. public static Connection connection;
  7. private Statement statement;
  8.  
  9. public MySQL(String host, String dataBase, String user, String password) {
  10. this.host = host;
  11. this.dataBase = dataBase;
  12. this.user = user;
  13. this.password = password;
  14. getConnection();
  15. }
  16.  
  17. public void getConnection() {
  18. try {
  19. this.connection = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + dataBase + "?autoReconnect=true", user, password);
  20. this.statement = connection.createStatement();
  21. System.out.println("[MySQL] The connection was sucesfully created!");
  22. } catch (SQLException e) {
  23. System.out.println("[MySQL] The connection was not sucesfully created! Mistake:" + e.getMessage());
  24. e.printStackTrace();
  25. }
  26. }
  27.  
  28. public void closeConnection() {
  29. try {
  30. if (connection != null) {
  31. connection.close();
  32. System.out.println("[MySQL] The connection was sucesfully closed!");
  33. }
  34. } catch (SQLException e) {
  35. System.out.println("[MySQL] The connection was not sucesfully closed! Mistake: " + e.getMessage());
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. public void updateTable(String querry) {
  41. try {
  42. Statement statement = connection.createStatement();
  43. statement.executeUpdate(querry);
  44. statement.close();
  45. } catch (SQLException e) {
  46. getConnection();
  47. System.err.println(e);
  48. }
  49. }
  50.  
  51. public ResultSet querry(String querry) {
  52.  
  53. ResultSet resultSet = null;
  54.  
  55. try {
  56. Statement statement = connection.createStatement();
  57. resultSet = statement.executeQuery(querry);
  58. } catch (SQLException e) {
  59. getConnection();
  60. System.err.println(e);
  61. }
  62. return resultSet;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement