Advertisement
Guest User

MySQL

a guest
Jun 13th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package de.killerchecker.lm.mysql;
  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. private String HOST = "";
  11. private String DATABASE = "";
  12. private String USER = "";
  13. private String PASSWORD = "";
  14.  
  15. private Connection con;
  16.  
  17. public MySQL(String host, String database, String user, String password){
  18. this.HOST = host;
  19. this.DATABASE = database;
  20. this.USER = user;
  21. this.PASSWORD = password;
  22.  
  23. connect();
  24. }
  25.  
  26. public void connect() {
  27. try {
  28. con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  29. System.out.println("[MySQL] Verbindung zur MySQL wurde hergestellt");
  30. } catch (SQLException e) {
  31. System.out.println("[MySQL] Verbindung zur MySQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  32. }
  33. }
  34.  
  35. public void close(){
  36. try {
  37. if(con != null){
  38. con.close();
  39. System.out.println("[MySQL] Verbindung zur MySQL wurde erfolgreich beendet");
  40. }
  41. } catch (SQLException e) {
  42. System.out.println("[MySQL] Verbindung zur MySQL beenden ist fehlgeschlagen! Fehler: " + e.getMessage());
  43. }
  44. }
  45.  
  46. public void update(String qry){
  47. try {
  48. java.sql.Statement st = con.createStatement();
  49. st.executeUpdate(qry);
  50. st.close();
  51. } catch (SQLException e) {
  52. connect();
  53. System.err.println(e);
  54. }
  55. }
  56.  
  57. public ResultSet query(String qry){
  58. ResultSet rs = null;
  59.  
  60. try {
  61. java.sql.Statement st = con.createStatement();
  62. rs = st.executeQuery(qry);
  63. } catch (SQLException e) {
  64. connect();
  65. System.err.println(e);
  66. }
  67. return rs;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement