Guest User

Untitled

a guest
May 26th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package be.darthania.darthaentreprise.bdd;
  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 SqlConnection
  12. {
  13. private Connection connection;
  14. private String urlbase, host, database, user, pass;
  15.  
  16. public SqlConnection(String urlbase, String host, String database, String user, String pass)
  17. {
  18. this.urlbase = urlbase;
  19. this.host = host;
  20. this.database = database;
  21. this.user = user;
  22. this.pass = pass;
  23. }
  24.  
  25. public void connection()
  26. {
  27. if(!isConnected())
  28. {
  29. try
  30. {
  31. connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
  32. System.out.println("[DARTHA-ENTREPRISE] Connection a la BDD..");
  33. }
  34. catch (SQLException e)
  35. {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. public void disconnect()
  42. {
  43. if(isConnected())
  44. {
  45. try
  46. {
  47. connection.close();
  48. System.out.println("[DARTHA-ENTREPRISE] Deconnection de la BDD..");
  49. }
  50. catch (SQLException e)
  51. {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56.  
  57. public boolean isConnected()
  58. {
  59. return connection != null;
  60. }
  61.  
  62. public void createAccount(Player joueur)
  63. {
  64. if(!hasAccount(joueur))
  65. {
  66. try
  67. {
  68. PreparedStatement q = connection.prepareStatement("INSERT INTO ent_joueurs (uuid, joueur) VALUES(?, ?)");
  69. q.setString(1, joueur.getUniqueId().toString());
  70. q.setString(2, joueur.getName());;
  71. q.execute();
  72. q.close();
  73. }
  74. catch (SQLException e)
  75. {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80.  
  81. public boolean hasAccount(Player joueur)
  82. {
  83. try
  84. {
  85. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM ent_joueurs WHERE uuid = ?");
  86. q.setString(1, joueur.getUniqueId().toString());
  87. ResultSet resultat = q.executeQuery();
  88. boolean hasAccount = resultat.next();
  89. q.close();
  90. return hasAccount;
  91.  
  92. }
  93. catch (SQLException e)
  94. {
  95. e.printStackTrace();
  96. }
  97. return false;
  98. }
  99.  
  100. public void createEnterprise(Player joueur)
  101. {
  102. if(!hasEnterprise(joueur))
  103. {
  104. try
  105. {
  106. PreparedStatement rs = connection.prepareStatement("UPDATE ent_joueur SET hasEntreprise = ? WHERE uuid = ?");
  107. rs.setBoolean(1, true);
  108. rs.setString(2, joueur.getUniqueId().toString());
  109. rs.executeUpdate();
  110. rs.close();
  111. }
  112. catch (SQLException e)
  113. {
  114. e.printStackTrace();
  115. }
  116. }
  117. }
  118.  
  119. public boolean hasEnterprise(Player joueur)
  120. {
  121. if(hasAccount(joueur))
  122. {
  123. try
  124. {
  125. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM ent_joueurs WHERE uuid = ?");
  126. q.setString(1, joueur.getUniqueId().toString());
  127.  
  128. boolean hasEnterprise = false;
  129. ResultSet rs = q.executeQuery();
  130.  
  131. while(rs.next())
  132. {
  133. hasEnterprise = rs.getBoolean("entreprise");
  134. }
  135. q.close();
  136. return hasEnterprise;
  137.  
  138. }
  139. catch (SQLException e)
  140. {
  141. e.printStackTrace();
  142. }
  143. }
  144. return false;
  145. }
  146. }
Add Comment
Please, Sign In to add comment