Advertisement
Guest User

Untitled

a guest
Mar 5th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package de.hansehans.divewars.mysql;
  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 Database
  10. {
  11. private String database;
  12. private String host;
  13. private String port;
  14. private String username;
  15. private String password;
  16. private Connection con;
  17.  
  18. public Database(String host, String port, String username, String password, String name)
  19. {
  20. this.host = host;
  21. this.port = port;
  22. this.username = username;
  23. this.password = password;
  24. this.database = name;
  25. }
  26.  
  27. public void connect()
  28. {
  29. if (this.con == null) {
  30. try
  31. {
  32. this.con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.username, this.password);
  33. }
  34. catch (SQLException e)
  35. {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. public void disconnect()
  42. {
  43. if (this.con != null) {
  44. try
  45. {
  46. this.con.close();
  47. }
  48. catch (SQLException e)
  49. {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54.  
  55. public void update(String query)
  56. {
  57. try
  58. {
  59. Statement st = this.con.createStatement();
  60. st.executeUpdate(query);
  61. st.close();
  62. }
  63. catch (SQLException e)
  64. {
  65. e.printStackTrace();
  66. }
  67. }
  68.  
  69. public ResultSet updateWithResult(String query)
  70. {
  71. ResultSet rs = null;
  72. try
  73. {
  74. Statement st = this.con.createStatement();
  75. rs = st.executeQuery(query);
  76. }
  77. catch (SQLException e)
  78. {
  79. e.printStackTrace();
  80. }
  81. return rs;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement