SHARE
TWEET

Untitled

a guest Dec 13th, 2016 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import org.bukkit.entity.Player;
  9.  
  10. public class SqlConnection {
  11.  
  12.    private Connection connection;
  13.    private String urlbase,host,database,user,pass;
  14.  
  15.    public SqlConnection(String urlbase, String host, String database, String user, String pass) {
  16.        this.urlbase = urlbase;
  17.        this.host = host;
  18.        this.database = database;
  19.        this.user = user;
  20.        this.pass = pass;
  21.    }
  22.  
  23.    public void connection(){
  24.        if(!isConnected()){
  25.            try {
  26.                connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
  27.                System.out.println("connected ok");
  28.            } catch (SQLException e) {
  29.                e.printStackTrace();
  30.            }
  31.        }
  32.    }
  33.  
  34.    public void disconnect(){
  35.        if(isConnected()){
  36.            try {
  37.                connection.close();
  38.                System.out.println("connected off");
  39.            } catch (SQLException e) {
  40.                e.printStackTrace();
  41.            }
  42.        }
  43.    }
  44.  
  45.    public boolean isConnected(){
  46.        return connection != null;
  47.    }
  48.  
  49. public void createAccount(Player player){
  50.     if(!hasAccount(player)){
  51.         //INSERT
  52.        
  53.         try {
  54.             PreparedStatement q = connection.prepareStatement("INSERT INTO joueurs(uuid,coins,grade) VALUES (?,?,?)");
  55.             q.setString(1, player.getUniqueId().toString());
  56.             q.setInt(2, 100);
  57.             q.setString(3, "joueur");
  58.             q.execute();
  59.             q.close();
  60.         } catch (SQLException e) {
  61.             e.printStackTrace();
  62.         }
  63.        
  64.        
  65.     }
  66. }
  67.  
  68. public boolean hasAccount(Player player){
  69.     //SELECT
  70.    
  71.     try {
  72.         PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
  73.         q.setString(1, player.getUniqueId().toString());
  74.         ResultSet resultat = q.executeQuery();
  75.         boolean hasAccount = resultat.next();
  76.         q.close();
  77.         return hasAccount;
  78.     } catch (SQLException e) {
  79.         e.printStackTrace();
  80.     }
  81.    
  82.     return false;
  83. }
  84.  
  85. public int getBalance(Player player){
  86.     //SELECT
  87.    
  88.     try {
  89.         PreparedStatement q = connection.prepareStatement("SELECT coins FROM joueurs WHERE uuid = ?");
  90.         q.setString(1, player.getUniqueId().toString());
  91.  
  92.         int balance = 0;
  93.         ResultSet rs = q.executeQuery();
  94.        
  95.         while(rs.next()){
  96.             balance = rs.getInt("coins");
  97.         }
  98.        
  99.         q.close();
  100.        
  101.         return balance;
  102.        
  103.     } catch (SQLException e) {
  104.         e.printStackTrace();
  105.     }
  106.    
  107.     return 0;
  108. }
  109.  
  110. public void addMoney(Player player,int amount){
  111.     //UPDATE
  112.    
  113.     int balance = getBalance(player);
  114.     int newbalance = balance + amount;
  115.    
  116.     try {
  117.         PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET coins = ? WHERE uuid = ?");
  118.         rs.setInt(1, newbalance);
  119.         rs.setString(2, player.getUniqueId().toString());
  120.         rs.executeUpdate();
  121.         rs.close();
  122.  
  123.     } catch (SQLException e) {
  124.         e.printStackTrace();
  125.     }
  126.    
  127. }
  128.  
  129. public void removeMoney(Player player,int amount){
  130.     //UPDATE
  131.    
  132.     int balance = getBalance(player);
  133.     int newbalance = balance - amount;
  134.    
  135.     if(newbalance <= 0){
  136.         return;
  137.     }
  138.    
  139.     try {
  140.         PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET coins = ? WHERE uuid = ?");
  141.         rs.setInt(1, newbalance);
  142.         rs.setString(2, player.getUniqueId().toString());
  143.         rs.executeUpdate();
  144.         rs.close();
  145.  
  146.     } catch (SQLException e) {
  147.         e.printStackTrace();
  148.     }
  149.    
  150. }
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top