Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1.  
  2. public class MySQL {
  3.  
  4. public static String host = "TrySoul.de";
  5. public static String database = "db";
  6. public static String user = "root";
  7. public static String password = "123456";
  8.  
  9. private static Connection con;
  10.  
  11. public MySQL() {
  12.  
  13. connect();
  14. }
  15.  
  16. public static void connect() {
  17. try {
  18. con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database + "?autoReconnect=true", user, password);
  19. System.out.println("[MySQL] Erfolgreich verbunden.");
  20. } catch (SQLException ex) {
  21. ex.printStackTrace();
  22. System.out.println("[MySQL] Verbinden fehlgeschlagen!");
  23. }
  24. }
  25.  
  26. public static void close() {
  27. try {
  28. if(con != null) {
  29. con.close();
  30. }
  31. } catch(SQLException ex) {
  32. ex.printStackTrace();
  33. }
  34. }
  35.  
  36. public static ResultSet query(String qry) {
  37. ResultSet rs = null;
  38.  
  39. try {
  40. Statement st = con.createStatement();
  41. rs = st.executeQuery(qry);
  42. } catch(SQLException ex) {
  43. connect();
  44. ex.printStackTrace();
  45. }
  46.  
  47. return rs;
  48. }
  49.  
  50. public static void update(String qry) {
  51. try {
  52. Statement st = con.createStatement();
  53. st.executeUpdate(qry);
  54. st.close();
  55. } catch(SQLException ex) {
  56. connect();
  57. ex.printStackTrace();
  58. }
  59. }
  60.  
  61. public static Connection getConnection(){
  62. return con;
  63.  
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement