Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. private class QueryRunner {
  2.  
  3. String hostname, port, username, password;
  4. int retries;
  5. Connection conn = null;
  6. Statement s = null;
  7.  
  8. public QueryRunner(String hostname, String port, String username, String password, int retries) {
  9. this.hostname = hostname;
  10. this.port = port;
  11. this.username = username;
  12. this.password = password;
  13. this.retries = retries;
  14. }
  15.  
  16. private void connect() {
  17. try {
  18. String url = "jdbc:mysql://" + hostname + ":" + port + "/";
  19. conn = DriverManager.getConnection(url, username, password);
  20. s = conn.createStatement();
  21. } catch (SQLException sqle) {
  22. sqle.printStackTrace();
  23. }
  24. }
  25.  
  26. private boolean refresh() {
  27. int tries = 0;
  28. boolean buf = false;
  29. try {
  30. while (!(buf = conn.isClosed()) && tries < retries) {
  31. tries++;
  32. connect();
  33. }
  34. } catch (SQLException sqle) {
  35. sqle.printStackTrace();
  36. }
  37. return buf;
  38. }
  39.  
  40. public boolean query(String q) {
  41. boolean buf = false;
  42. try {
  43. if (!refresh()) {
  44. ResultSet rs = s.executeQuery(q);
  45. buf = (rs != null);
  46. }
  47. } catch (SQLException sqle) {
  48. sqle.printStackTrace();
  49. }
  50. return buf;
  51. }
  52.  
  53. public void end() {
  54. try {
  55. s.close();
  56. conn.close();
  57. } catch (SQLException sqle) {
  58. sqle.printStackTrace();
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement