Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package MySQL;
  2.  
  3. import java.sql.*;
  4.  
  5. public class MySQLConnect
  6. {
  7. public static String HOST = "";
  8. public static String DATABASE = "";
  9. public static String USER = "";
  10. public static String PASSWORD = "";
  11. private Connection con;
  12.  
  13. public MySQLConnect(String host, String database, String user, String password)
  14. {
  15. HOST = host;
  16. DATABASE = database;
  17. USER = user;
  18. PASSWORD = password;
  19.  
  20. connect();
  21. }
  22.  
  23. public void connect()
  24. {
  25. try
  26. {
  27. this.con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  28. //Erfolgreich
  29. }
  30. catch (SQLException e)
  31. {
  32. //Verbindung fehlgeschlagen
  33. }
  34. }
  35.  
  36. public void close()
  37. {
  38. try
  39. {
  40. if (this.con != null) {
  41. this.con.close();
  42. }
  43. }
  44. catch (SQLException localSQLException) {}
  45. }
  46.  
  47. public void update(String qry)
  48. {
  49. try
  50. {
  51. Statement st = this.con.createStatement();
  52. st.executeUpdate(qry);
  53. st.close();
  54. }
  55. catch (SQLException e)
  56. {
  57. connect();
  58. System.err.println(e);
  59. }
  60. }
  61.  
  62. public ResultSet query(String qry)
  63. {
  64. ResultSet rs = null;
  65. try
  66. {
  67. Statement st = this.con.createStatement();
  68. rs = st.executeQuery(qry);
  69. }
  70. catch (SQLException e)
  71. {
  72. connect();
  73. System.err.println(e);
  74. }
  75. return rs;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement