Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package me.quickscythe.hyversecore.utils;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9.  
  10. /**
  11. * The IDatabase is a simple class that adds all the utils you need to communicate with an SQL server.
  12. *
  13. * @author Cameron Witcher
  14. * @version 1.0
  15. * @since 2016-12-09
  16. */
  17. public class IDatabase {
  18.  
  19.  
  20.  
  21. public Connection connection;
  22. private Properties properties;
  23. private String user;
  24. private String pass;
  25. private String url;
  26.  
  27. public IDatabase(String host, String database, Integer port, String username, String password) {
  28. this.properties = new Properties();
  29. this.user = username;
  30. this.pass = password;
  31. this.properties.setProperty("user", username);
  32. this.properties.setProperty("password", password);
  33. this.url = "jdbc:mysql://" + host + ":" + port + "/" + database;
  34. }
  35.  
  36. public Boolean init() {
  37. try {
  38. // String url = "jdbc:mysql://localhost:3306/test";
  39. // Properties info = new Properties();
  40. // info.put("user", "root");
  41. // info.put("password", "test");
  42. Class.forName("com.mysql.jdbc.Driver");
  43. connection = DriverManager.getConnection(url, user, pass);
  44.  
  45. if (connection != null) {
  46. System.out.println("Successfully connected to MySQL database test");
  47. return true;
  48. }
  49.  
  50. } catch (SQLException | ClassNotFoundException ex) {
  51. System.out.println("An error occurred while connecting MySQL databse");
  52. ex.printStackTrace();
  53. return false;
  54. }
  55. // try {
  56. // if (connection != null && !connection.isClosed()) {
  57. // return true;
  58. // }
  59. // Class.forName("com.mysql.jbdc.Driver");
  60. // this.connection = DriverManager.getConnection(url, user, pass);
  61. // return true;
  62. // } catch (Exception exception) {
  63. // exception.printStackTrace();
  64. // return false;
  65. return false;
  66. // }
  67. }
  68.  
  69. public ResultSet query(String query, Object... values) {
  70. try {
  71. PreparedStatement statement = prepare(query, values);
  72. if (statement == null) {
  73. return null;
  74. }
  75. return statement.executeQuery();
  76. } catch (Exception exception) {
  77. return null;
  78. }
  79. }
  80.  
  81. public Integer update(String query, Object... values) {
  82. try {
  83. PreparedStatement statement = prepare(query, values);
  84. if (statement == null) {
  85. return -1;
  86. }
  87. return statement.executeUpdate();
  88. } catch (Exception exception) {
  89. exception.printStackTrace();
  90. return -1;
  91. }
  92.  
  93.  
  94.  
  95. }
  96.  
  97. public boolean input(String query, Object... values) {
  98. try {
  99. PreparedStatement statement = prepare(query, values);
  100. if (statement == null) {
  101. return false;
  102. }
  103. return statement.execute();
  104. } catch (Exception exception) {
  105. return false;
  106. }
  107.  
  108.  
  109.  
  110. }
  111.  
  112. private PreparedStatement prepare(String query, Object... values) {
  113. try {
  114. if (!init()) {
  115. return null;
  116. }
  117. PreparedStatement statement = connection.prepareStatement(query);
  118. for (int i = 0; i < values.length; i++) {
  119. statement.setObject(i + 1, values[i]);
  120. }
  121. return statement;
  122. } catch (Exception exception) {
  123. return null;
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement