Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package fr.cercus.adventsky.SQL;
  2.  
  3. import java.sql.Connection;
  4.  
  5. import java.sql.DriverManager;
  6.  
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import org.bukkit.entity.Player;
  12.  
  13.  
  14. public class SqlConnection {
  15.    
  16.     private Connection connection;
  17.     private String urlbase,host,database,user,password;
  18.  
  19.     public SqlConnection(String urlbase, String host, String database, String user, String password) {
  20.         this.urlbase = urlbase;
  21.         this.host = host;
  22.         this.database = database;
  23.         this.user = user;
  24.         this.password = password;
  25.     }
  26.  
  27.     public void connection() {
  28.         if(!isConnected()) {
  29.            
  30.             try {
  31.                 System.out.print("Connection to the database in progress...");
  32.                 connection = DriverManager.getConnection(urlbase + host + "/" + database, user, password);
  33.                 System.out.println("Connection established");
  34.             } catch (SQLException e) {
  35.                 System.out.println("Error while connection with the database...");
  36.                 e.printStackTrace();
  37.             }
  38.         }
  39.     }
  40.  
  41.     public void disconnect() {
  42.         if(!isConnected()) {
  43.             try {
  44.                 connection.close();
  45.                 System.out.println("Shutdown the connection to the database in progress");
  46.             } catch (SQLException e) {
  47.                 System.out.println("Error while disconnection with the database");
  48.                 e.printStackTrace();
  49.             }
  50.         }
  51.        
  52.     }
  53.    
  54.     public boolean isConnected(){
  55.         return connection != null;
  56.     }
  57.    
  58.     public void createAccount(Player player) {
  59.         if(!hasAccount(player)) {
  60.             try {
  61.                 PreparedStatement q = connection.prepareStatement("INSERT INTO joueurs(pseudo, uuid, money, grades, rang) VALUES(?,?,?,?,?)");
  62.                 q.setString(1, player.getName().toString());
  63.                 q.setString(2, player.getUniqueId().toString());
  64.                 q.setInt(3, 100);
  65.                 q.setString(4, "Joueur");
  66.                 q.setString(5, "L");
  67.                 q.execute();
  68.                 q.close();
  69.             } catch (SQLException e) {
  70.                 // TODO Auto-generated catch block
  71.                 e.printStackTrace();
  72.             }
  73.         }
  74.        
  75.     }
  76.    
  77.     public boolean hasAccount(Player player) {
  78.         try {
  79.             PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
  80.             q.setString(1, player.getUniqueId().toString());
  81.             ResultSet resultat = q.executeQuery();
  82.             boolean hasAccount = resultat.next();
  83.             q.close();
  84.             return hasAccount;
  85.         } catch (SQLException e) {
  86.             // TODO Auto-generated catch block
  87.             e.printStackTrace();
  88.         }
  89.         return false;
  90.     }
  91.    
  92.  
  93. }
  94.  
  95.  
  96. package fr.cercus.adventsky.SQL;
  97.  
  98. import org.bukkit.entity.Player;
  99.  
  100. import org.bukkit.event.EventHandler;
  101. import org.bukkit.event.Listener;
  102. import org.bukkit.event.player.PlayerJoinEvent;
  103. import org.bukkit.plugin.java.JavaPlugin;
  104.  
  105. public class Core extends JavaPlugin implements Listener{
  106.    
  107. public SqlConnection sql;
  108.    
  109.     public void onEnable(){
  110.         sql = new SqlConnection("jdbc:mysql://","localhost","adventsk_serveur","adventsk_admin","Lali-ho4");
  111.         sql.connection();
  112.         getServer().getPluginManager().registerEvents(this, this);
  113.        
  114.     }
  115.     public void onDisable(){
  116.         sql.disconnect();
  117.     }
  118.    
  119.     @EventHandler
  120.     public void join(PlayerJoinEvent e) {
  121.         Player p = e.getPlayer();
  122.         //On appelle le createAccount
  123.         sql.createAccount(p);
  124.     }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement