Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package supportManager;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. /**
  12. * @author Gamer08 30.10.2017
  13. */
  14. public class MysqlConnection {
  15.  
  16. public static String host = "localhost";
  17. public static String port = "3306";
  18. public static String database = "tickets";
  19. public static String username = "root";
  20. public static String password = "";
  21. public static Connection con;
  22.  
  23. public static boolean isConnected() {
  24. return con != null;
  25. }
  26.  
  27. public static void connect() {
  28. if (!isConnected()) {
  29. try {
  30. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username,password);
  31. } catch (SQLException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35.  
  36. }
  37. }
  38.  
  39. public static void disconnect() {
  40. try {
  41. con.close();
  42. } catch (SQLException e) {
  43. }
  44. }
  45.  
  46. public static PreparedStatement getStatement(String sql) {
  47. if (isConnected()) {
  48. PreparedStatement ps;
  49. try {
  50. ps = con.prepareStatement(sql);
  51. return ps;
  52.  
  53. } catch (SQLException e) {
  54. // TODO: handle exception
  55. e.printStackTrace();
  56. }
  57.  
  58. }
  59. return null;
  60.  
  61. }
  62.  
  63. public static ResultSet getResult(String sql) {
  64. if (isConnected()) {
  65. PreparedStatement ps;
  66. ResultSet rs;
  67.  
  68. try {
  69. ps = getStatement(sql);
  70. rs = ps.executeQuery();
  71. return rs;
  72. } catch (SQLException e) {
  73. // TODO: handle exception
  74. e.printStackTrace();
  75. }
  76. }
  77.  
  78. return null;
  79.  
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement