Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1.  
  2.  
  3. public static String host;
  4. public static String port;
  5. public static String database;
  6. public static String username;
  7. public static String password;
  8. public static Connection con;
  9.  
  10. public static void connect(){
  11. if(!isConnected()){
  12. try {
  13. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database+"?autoReconnect=true", username, password);
  14. System.out.println( Cache.ANSI_GREEN + "[MySQL] Die Verbindung zu MySQL wurde aufgebaut!" + Cache.ANSI_WHITE);
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20.  
  21. public static void disconnect(){
  22. if(isConnected()){
  23. try {
  24. con.close();
  25. System.out.println(Cache.ANSI_RED + "[MySQL] Die Verbindung wurde geschlossen!" + Cache.ANSI_WHITE);
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31.  
  32. public static boolean isConnected(){
  33. return (con == null ? false : true);
  34. }
  35.  
  36. public static Connection getConnection(){
  37. return con;
  38. }
  39.  
  40. public static void update(String qry){
  41. if(isConnected()){
  42. try {
  43. con.createStatement().execute(qry);
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49.  
  50. public static ResultSet getResult(String qry){
  51. ResultSet rs = null;
  52. if(isConnected()) {
  53. try {
  54. return con.createStatement().executeQuery(qry);
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. } else {
  59. connect();
  60. try {
  61. return con.createStatement().executeQuery(qry);
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. return rs;
  67. }
  68.  
  69. public static Connection getCon() {
  70. return con;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement