Advertisement
Guest User

Untitled

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