Advertisement
Guest User

SQLAPI

a guest
Sep 13th, 2016
82
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.imwazzix.antiff;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import org.bukkit.entity.Player;
  10.  
  11. public class SQLAPI {
  12.  
  13. private String url_base, host, name, username, password;
  14. private Connection connection;
  15.  
  16. public SQLAPI(String url_base, String host, String name, String username,
  17. String password) {
  18.  
  19. this.url_base = url_base;
  20. this.host = host;
  21. this.name = name;
  22. this.username = username;
  23. this.password = password;
  24.  
  25. }
  26.  
  27. public void Connection() {
  28. if (!isConnected()) {
  29. try {
  30. connection = DriverManager.getConnection(url_base + host + "/"
  31. + name, username, password);
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37.  
  38. public void Unconnection() {
  39. if (isConnected()) {
  40. try {
  41. connection.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47.  
  48. private boolean isConnected() {
  49. try {
  50. if (connection == null || connection.isClosed()
  51. || !connection.isValid(5)) {
  52. return false;
  53. } else {
  54. return true;
  55. }
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. }
  59. return false;
  60. }
  61.  
  62. private Connection getConnection() {
  63. return connection;
  64. }
  65.  
  66. public String getServer(Player p) {
  67. String server = null;
  68. try {
  69. PreparedStatement sts = getConnection().prepareStatement(
  70. "SELECT server FROM choixserveur WHERE player = ?");
  71. sts.setString(1, p.getName());
  72. ResultSet rs = sts.executeQuery();
  73. if (!rs.next()) {
  74. return server;
  75. }
  76. server = rs.getString("server");
  77. sts.close();
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81. return server;
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement