Vinetos

Bukkit Dev' | #39 - Base de donnés - Main.java

Aug 13th, 2015
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import java.sql.PreparedStatement;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.util.HashMap;
  5.  
  6. import org.bukkit.event.EventHandler;
  7. import org.bukkit.event.Listener;
  8. import org.bukkit.event.player.AsyncPlayerChatEvent;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10.  
  11. public class Main extends JavaPlugin implements Listener{
  12.  
  13. private static HashMap<String, String> words = new HashMap<String, String>();
  14.  
  15. @Override
  16. public void onEnable() {
  17. getServer().getPluginManager().registerEvents(this, this);
  18. readAll();
  19. getCommand("mysql").setExecutor(new Commandes());
  20. }
  21.  
  22. @EventHandler
  23. public void onChat(AsyncPlayerChatEvent e){
  24. for(String s : words.keySet()){
  25. if(e.getMessage().contains(s)){
  26. e.setCancelled(true);
  27. e.getPlayer().sendMessage("§c"+words.get(s));
  28. }
  29. }
  30. }
  31.  
  32. public static void insert(String word, String phrase){
  33. MySQL.connection();
  34. PreparedStatement sts;
  35. try {
  36. sts = MySQL.getConnection().prepareStatement("INSERT INTO words (word, phrase) VALUES(?, ?)");
  37. sts.setString(1, word);
  38. sts.setString(2, phrase);
  39. sts.executeUpdate();
  40. sts.close();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. MySQL.deconnection();
  45. }
  46.  
  47. public static void readAll(){
  48. MySQL.connection();
  49. PreparedStatement sts;
  50. try {
  51. sts = MySQL.getConnection().prepareStatement("SELECT * FROM words");
  52. ResultSet result = sts.executeQuery();
  53. while(result.next()){
  54. String word, phrase;
  55. word = result.getString("word");
  56. phrase = result.getString("phrase");
  57. System.out.println("LE MOT: " +word+ " est sur la même ligne que "+phrase);
  58. words.put(word, phrase);
  59. }
  60. sts.close();
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. MySQL.deconnection();
  65. }
  66.  
  67. public static void read(String word){
  68. MySQL.connection();
  69. PreparedStatement sts;
  70. try {
  71. sts = MySQL.getConnection().prepareStatement("SELECT phrase FROM words WHERE word = ?");
  72. sts.setString(1, word);
  73. ResultSet result = sts.executeQuery();
  74. while(result.next()){
  75. String word_retour, phrase;
  76. word_retour = result.getString("word");
  77. phrase = result.getString("phrase");
  78. System.out.println("La phrase du mot " +word_retour+ " est: "+phrase);
  79. }
  80. sts.close();
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. MySQL.deconnection();
  85. }
  86.  
  87. public static void update(String word, String phrase){
  88. MySQL.connection();
  89. PreparedStatement sts;
  90. try {
  91. sts = MySQL.getConnection().prepareStatement("UPDATE words SET phrase = ? WHERE word = ?");
  92. sts.setString(1, phrase);
  93. sts.setString(2, word);
  94. sts.executeUpdate();
  95. sts.close();
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. }
  99. MySQL.deconnection();
  100. }
  101.  
  102. public static void delete(String word){
  103. MySQL.connection();
  104. PreparedStatement sts;
  105. try {
  106. sts = MySQL.getConnection().prepareStatement("DELETE FROM words WHERE word = ?");
  107. sts.setString(1, word);
  108. sts.executeUpdate();
  109. sts.close();
  110. } catch (SQLException e) {
  111. e.printStackTrace();
  112. }
  113. MySQL.deconnection();
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment