Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. public static Database mysql;
  2. private DatabaseInformation dbinfo = new DatabaseInformation();
  3. private String host = dbinfo.host;
  4. private String port = dbinfo.port;
  5. private String database = dbinfo.database;
  6. private String username = dbinfo.user;
  7. private String password = dbinfo.pass;
  8. private Connection con;
  9.  
  10. public Connection openConnection(String database) throws SQLException {
  11. return DriverManager.getConnection(
  12. "jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false", username, password);
  13. }
  14.  
  15. public Connection openConnection() throws SQLException {
  16. return DriverManager.getConnection(
  17. "jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false", username, password);
  18. }
  19.  
  20. public void closeConnection(Connection c) throws SQLException {
  21.  
  22. if (c != null && c.isClosed()) {
  23. c.close();
  24. }
  25. }
  26.  
  27. public PreparedStatement prepareStatement(String qry) {
  28. PreparedStatement st = null;
  29. try {
  30. st = con.prepareStatement(qry);
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. return st;
  35. }
  36.  
  37. public void update(PreparedStatement statement) {
  38. try {
  39. statement.executeUpdate();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. try {
  43. statement.close();
  44. } catch (SQLException e1) {
  45. e1.printStackTrace();
  46. }
  47. } finally {
  48. try {
  49. statement.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55.  
  56. public boolean checkQuery(String sql) {
  57. try {
  58. return !con.createStatement().executeQuery(sql).next();
  59. } catch (SQLException e) {
  60. }
  61. return false;
  62. }
  63.  
  64. public boolean hasConnection() {
  65. try {
  66. return !con.isClosed();
  67. } catch (Exception e) {
  68. }
  69. return false;
  70. }
  71.  
  72. public ResultSet query(String query) {
  73. try {
  74. Statement statement = con.createStatement();
  75. return statement.executeQuery(query);
  76. }
  77. catch (SQLException e) {
  78. e.printStackTrace(); }
  79. return null;
  80. }
  81.  
  82. public boolean update(String query) {
  83. try {
  84. Statement statement = con.createStatement();
  85. statement.executeUpdate(query);
  86. statement.close();
  87. return true;
  88. } catch (SQLException ex) {
  89. ex.printStackTrace();
  90. }
  91. return false;
  92. }
  93.  
  94. public boolean execute(PreparedStatement statement) {
  95. try {
  96. return statement.execute();
  97. } catch (SQLException e) {
  98. e.printStackTrace();
  99. }
  100. return false;
  101. }
  102.  
  103. public ResultSet query(PreparedStatement statement) {
  104. try {
  105. return statement.executeQuery();
  106. } catch (SQLException e) {
  107. e.printStackTrace();
  108. }
  109. return null;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement