Advertisement
Guest User

Untitled

a guest
Mar 8th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package utils;
  2.  
  3. import java.sql.*;
  4.  
  5. public class MySQL {
  6.  
  7.  
  8. public static String host = "localhost";
  9. public static String port = "3306";
  10. public static String database = "plugintest";
  11. public static String username = "root";
  12. public static String password = "puX8ABIYj6IQwN7n";
  13. public static Connection con;
  14.  
  15. public static void connect() {
  16. if (!isConnected()) {
  17. try {
  18. con = DriverManager.getConnection("jdbc:mysql://"
  19. + host +
  20. ":"
  21. + port
  22. + "/" +
  23. database, username, password);
  24. System.out.println("[MySQL] Verbindung aufgebaut!");
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. }
  31.  
  32. public static void disconnect() {
  33. if (isConnected()) {
  34. try {
  35. con.close();
  36. System.out.println("[MySQL] Verbindung getrennt!");
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42.  
  43. public static boolean isConnected() {
  44. return (con == null ? false : true);
  45.  
  46. }
  47.  
  48. public static void update(String qry) {
  49. try {
  50. PreparedStatement ps = con.prepareStatement(qry);
  51. ps.executeUpdate();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. public static ResultSet getResult(String qry) {
  58. try {
  59. PreparedStatement ps = con.prepareStatement(qry);
  60. return ps.executeQuery();
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. return null;
  65.  
  66.  
  67. }
  68.  
  69. public static ResultSet query(String qry) {
  70. ResultSet rs = null;
  71.  
  72. try {
  73. Statement st = con.createStatement();
  74. rs = st.executeQuery(qry);
  75. } catch (SQLException e) {
  76. connect();
  77. System.err.println(e);
  78. }
  79. return rs;
  80. }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement