Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
3,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package MySQL;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8.  
  9. public class MySQL {
  10.  
  11. public static Connection con;
  12.  
  13. String host;
  14. String name;
  15. String password;
  16. String database;
  17.  
  18. public MySQL(String host, String user, String pw, String db) {
  19. this.host = host;
  20. this.name = user;
  21. this.password = pw;
  22. this.database = db;
  23. }
  24.  
  25. public void connect() {
  26. if(!isConnected()) {
  27. try {
  28. con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database + "?autoReconnect=true", name, password);
  29. System.out.println("[MySQL] Verbindung zur MySQL erfolgreich!");
  30.  
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. System.out.println("[MySQL] §4Fehler: §c" + e.getMessage());
  34.  
  35. }
  36.  
  37. }
  38. }
  39.  
  40. public void close() {
  41. if(isConnected()) {
  42. try {
  43. con.close();
  44. System.out.println("[MySQL] Verbindung zur MySQL beendet!");
  45.  
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. System.out.println("[MySQL] §4Fehler: §c" + e.getMessage());
  49.  
  50. }
  51. }
  52. }
  53.  
  54. public boolean isConnected() {
  55. return con != null;
  56.  
  57. }
  58.  
  59. public void createTable(String name, String table) {
  60. try {
  61. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS " + name + "(" + table + ")");
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. public void update(String qry) {
  68. if(isConnected()) {
  69. try {
  70. con.createStatement().executeUpdate(qry);
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }
  76.  
  77. public ResultSet getResult(String qry) {
  78. if(isConnected()) {
  79. try {
  80. return con.createStatement().executeQuery(qry);
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. return null;
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement