Advertisement
Guest User

MYSQL

a guest
Dec 1st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. /* */ public static String host;
  2. /* */ public static String port;
  3. /* */ public static String database;
  4. /* */ public static String username;
  5. /* */ public static String password;
  6. /* */ public static Connection con;
  7. /* */
  8. /* */ public static void connect()
  9. /* */ {
  10. /* 24 */ if (!isConnected()) {
  11. /* */ try {
  12. /* 26 */ con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  13. /* 27 */ System.out.println("\033[32m[MySQL] Die Verbindung zu MySQL wurde aufgebaut!\033[37m");
  14. /* */ } catch (SQLException e) {
  15. /* 29 */ e.printStackTrace();
  16. /* */ }
  17. /* */ }
  18. /* */ }
  19. /* */
  20. /* */ public static void disconnect() {
  21. /* 35 */ if (isConnected()) {
  22. /* */ try {
  23. /* 37 */ con.close();
  24. /* 38 */ System.out.println("\033[31m[MySQL] Die Verbindung wurde geschlossen!\033[37m");
  25. /* */ } catch (SQLException e) {
  26. /* 40 */ e.printStackTrace();
  27. /* */ }
  28. /* */ }
  29. /* */ }
  30. /* */
  31. /* */ public static boolean isConnected() {
  32. /* 46 */ return con != null;
  33. /* */ }
  34. /* */
  35. /* */ public static Connection getConnection() {
  36. /* 50 */ return con;
  37. /* */ }
  38. /* */
  39. /* */ public static void update(String query) {
  40. /* 54 */ PreparedStatement ps = null;
  41. /* */ try {
  42. /* 56 */ ps = getConnection().prepareStatement(query);
  43. /* 57 */ ps.executeUpdate(); return;
  44. /* */ } catch (SQLException e) {
  45. /* 59 */ e.printStackTrace();
  46. /* */ } finally {
  47. /* */ try {
  48. /* 62 */ ps.close();
  49. /* */ } catch (SQLException e) {
  50. /* 64 */ e.printStackTrace();
  51. /* */ }
  52. /* */ }
  53. /* */ }
  54. /* */
  55. /* */ public static String getString(String query, String object) {
  56. /* 70 */ PreparedStatement ps = null;
  57. /* 71 */ ResultSet rs = null;
  58. /* */ try {
  59. /* 73 */ ps = getConnection().prepareStatement(query);
  60. /* 74 */ rs = ps.executeQuery();
  61. /* 75 */ if (rs.next()) {
  62. /* 76 */ return rs.getString(object);
  63. /* */ }
  64. /* */
  65. /* */
  66. /* */
  67. /* */
  68. /* */
  69. /* */
  70. /* */
  71. /* */
  72. /* */
  73. /* */
  74. /* 88 */ return null;
  75. /* */ }
  76. /* */ catch (SQLException e)
  77. /* */ {
  78. /* 79 */ e.printStackTrace();
  79. /* */ } finally {
  80. /* */ try {
  81. /* 82 */ rs.close();
  82. /* 83 */ ps.close();
  83. /* */ } catch (SQLException e) {
  84. /* 85 */ e.printStackTrace();
  85. /* */ }
  86. /* */ }
  87. /* */ }
  88. /* */
  89. /* */ public static int getInt(String query, String object)
  90. /* */ {
  91. /* 92 */ PreparedStatement ps = null;
  92. /* 93 */ ResultSet rs = null;
  93. /* */ try {
  94. /* 95 */ ps = getConnection().prepareStatement(query);
  95. /* 96 */ rs = ps.executeQuery();
  96. /* 97 */ if (rs.next()) {
  97. /* 98 */ return rs.getInt(object);
  98. /* */ }
  99. /* */
  100. /* */
  101. /* */
  102. /* */
  103. /* */
  104. /* */
  105. /* */
  106. /* */
  107. /* */
  108. /* */
  109. /* 110 */ return 0;
  110. /* */ }
  111. /* */ catch (SQLException e)
  112. /* */ {
  113. /* 101 */ e.printStackTrace();
  114. /* */ } finally {
  115. /* */ try {
  116. /* 104 */ rs.close();
  117. /* 105 */ ps.close();
  118. /* */ } catch (SQLException e) {
  119. /* 107 */ e.printStackTrace();
  120. /* */ }
  121. /* */ }
  122. /* */ }
  123. /* */
  124. /* */
  125. /* */ public static ArrayList<String> getStringList(String query, String object)
  126. /* */ {
  127. /* 115 */ PreparedStatement ps = null;
  128. /* 116 */ ResultSet rs = null;
  129. /* */ try {
  130. /* 118 */ ArrayList<String> list = new ArrayList();
  131. /* 119 */ ps = getConnection().prepareStatement(query);
  132. /* 120 */ rs = ps.executeQuery();
  133. /* 121 */ while (rs.next()) {
  134. /* 122 */ list.add(rs.getString(object));
  135. /* */ }
  136. /* 124 */ return list;
  137. /* */ } catch (SQLException e) {
  138. /* 126 */ e.printStackTrace();
  139. /* */ } finally {
  140. /* */ try {
  141. /* 129 */ rs.close();
  142. /* 130 */ ps.close();
  143. /* */ } catch (SQLException e) {
  144. /* 132 */ e.printStackTrace();
  145. /* */ }
  146. /* */ }
  147. /* 135 */ return null;
  148. /* */ }
  149. /* */
  150. /* */ public static ArrayList<Integer> getIntList(String query, String object) {
  151. /* 139 */ PreparedStatement ps = null;
  152. /* 140 */ ResultSet rs = null;
  153. /* */ try {
  154. /* 142 */ ArrayList<Integer> list = new ArrayList();
  155. /* 143 */ ps = getConnection().prepareStatement(query);
  156. /* 144 */ rs = ps.executeQuery();
  157. /* 145 */ while (rs.next()) {
  158. /* 146 */ list.add(Integer.valueOf(rs.getInt(object)));
  159. /* */ }
  160. /* 148 */ return list;
  161. /* */ } catch (SQLException e) {
  162. /* 150 */ e.printStackTrace();
  163. /* */ } finally {
  164. /* */ try {
  165. /* 153 */ rs.close();
  166. /* 154 */ ps.close();
  167. /* */ } catch (SQLException e) {
  168. /* 156 */ e.printStackTrace();
  169. /* */ }
  170. /* */ }
  171. /* 159 */ return null;
  172. /* */ }
  173. /* */
  174. /* */ public static ResultSet getResult(String query) {
  175. /* 163 */ PreparedStatement ps = null;
  176. /* 164 */ ResultSet rs = null;
  177. /* */ try {
  178. /* 166 */ ps = getConnection().prepareStatement(query);
  179. /* 167 */ return ps.executeQuery();
  180. /* */ }
  181. /* */ catch (SQLException e) {
  182. /* 170 */ e.printStackTrace();
  183. /* */ }
  184. /* 172 */ return null;
  185. /* */ }
  186. /* */
  187. /* */ public static Connection getCon() {
  188. /* 176 */ return con;
  189. /* */ }
  190. /* */ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement