Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package fr.algathia.algathiaapi.utils;
  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. import org.bukkit.Bukkit;
  10.  
  11. import fr.algathia.algathiaapi.AlgathiaAPI;
  12. import net.md_5.bungee.BungeeCord;
  13.  
  14. public class SQL {
  15. protected static Connection conn;
  16. static String ip, database, userName, password;
  17. static int port;
  18.  
  19. public SQL(String ip, int port, String database, String userName, String password) {
  20. SQL.ip = ip;
  21. SQL.port = port;
  22. SQL.database = database;
  23. SQL.userName = userName;
  24. SQL.password = password;
  25. connect();
  26. }
  27.  
  28. private static void connect() {
  29. try {
  30. Class.forName("com.mysql.jdbc.Driver");
  31. } catch (ClassNotFoundException e) {
  32. e.printStackTrace();
  33. }
  34. try {
  35. conn = DriverManager.getConnection("jdbc:mysql://" + ip + ":" + port + "/" + database, userName, password);
  36. System.out.println("SQL Connection work !");
  37. } catch (SQLException e) {
  38. System.out.println("SQL Connection don't work !");
  39. e.printStackTrace();
  40. if(AlgathiaAPI.isSpigot())
  41. Bukkit.shutdown();
  42. else
  43. BungeeCord.getInstance().stop();
  44. }
  45. }
  46.  
  47. public static void writedata(String sql) {
  48. try {
  49. if (conn.isClosed()) {
  50. connect();
  51. }
  52. Statement state = conn.createStatement();
  53. state.executeUpdate(sql);
  54. state.close();
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58.  
  59. }
  60.  
  61. public static ResultSet getdata(String sql) {
  62. ResultSet result = null;
  63. try {
  64. if (conn.isClosed()) {
  65. connect();
  66. }
  67. Statement state = conn.createStatement();
  68. result = state.executeQuery(sql);
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. }
  72. return result;
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement