Advertisement
NiRoXz

MySQL-Class

Mar 9th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package de.gamechest.bansystem.niroxz.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. public static String user;
  11. public static String password;
  12. public static String database;
  13. public static String host;
  14. public static String port;
  15. public static Connection con;
  16.  
  17. public static void connect() {
  18. if (!isConnected()) {
  19. try {
  20. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
  21. System.out.println("[Ban MySQL] Connecting to: "+host + ":" + port + "/" + database);
  22. } catch (SQLException e) {
  23. }
  24. }
  25. }
  26.  
  27. public static void close() {
  28. if(isConnected()) {
  29. try {
  30. con.close();
  31. System.out.println("[Ban MySQL] MySQL connection closed!");
  32. con = null;
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. System.err.println("[Ban MySQL] Could not close MySQL connection!");
  36. }
  37. }
  38. }
  39.  
  40. public static boolean isConnected()
  41. {
  42. return con != null;
  43. }
  44.  
  45. public static void createTable() {
  46. if (isConnected())
  47. try {
  48. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS BannedPlayers (id INT auto_increment, Spielername VARCHAR(100), UUID VARCHAR(100), IP VARCHAR(100),Ende VARCHAR(100) , Grund VARCHAR(100), Ersteller VARCHAR(100), PRIMARY KEY(id))");
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. public static void update(String qry)
  55. {
  56. if (isConnected())
  57. try {
  58. con.createStatement().execute(qry);
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public static ResultSet getResult(String qry)
  65. {
  66. if (isConnected()) {
  67. try {
  68. return con.createStatement().executeQuery(qry);
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. return null;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement