Advertisement
Guest User

Untitled

a guest
Jan 5th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. package main;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class MySQL {
  9.  
  10. public static String Host ="";
  11. public static String Database = "";
  12. public static String User = "";
  13. public static String Password = "";
  14.  
  15. private static Connection con;
  16.  
  17. public MySQL(String host, String database, String user, String password) {
  18. MySQL.Host = host;
  19. MySQL.Database = database;
  20. MySQL.User = user;
  21. MySQL.Password = password;
  22.  
  23. connect();
  24. }
  25.  
  26. public static void connect() {
  27. if(!isConnected()){
  28. try {
  29. con = DriverManager.getConnection("jdbc:mysql://" + Host + ":3306/" + Database + "?autoReconnect=true", User, Password);
  30. System.out.println("[MySQL] Die Verbindung zur MySQL wurde hergestellt!");
  31. } catch (SQLException e) {
  32. System.out.println("[MySQL] Die Verbindung zur MySQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  33. }
  34. }
  35. }
  36.  
  37. public static void close() {
  38. if(!isConnected()){
  39. try {
  40. con.close();
  41. System.out.println("[MySQL] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
  42. } catch (SQLException e){
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47.  
  48. public static boolean isConnected(){
  49. return con != null;
  50. }
  51.  
  52. public void update(String qry) {
  53. if(isConnected()){
  54. try {
  55. con.createStatement().executeUpdate(qry);
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61.  
  62. public static ResultSet getResult(String qry) {
  63. if(isConnected()){
  64. try {
  65. return con.createStatement().executeQuery(qry);
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. return null;
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement