Guest User

MySQL.java

a guest
Oct 23rd, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package me.snics.straf;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.util.HashMap;
  7.  
  8. import org.bukkit.plugin.Plugin;
  9.  
  10. import com.mysql.jdbc.PreparedStatement;
  11.  
  12. public abstract class MySQL implements Plugin
  13. {
  14.     static Connection connection;
  15.     String prefix = "§f§l[§c§lLABEL§f§l] §r";
  16.     static HashMap<String, String> playerPunish = new HashMap<String, String>();
  17.     static HashMap<String, String> punisher = new HashMap<String, String>();
  18.     static HashMap<String, String> reason = new HashMap<String, String>();
  19.    
  20.     public synchronized void onDisable()
  21.     {
  22.         try
  23.         {
  24.             if(connection != null && !connection.isClosed())
  25.             {
  26.                 connection.close();
  27.             }
  28.         }
  29.        
  30.         catch (Exception e1)
  31.         {
  32.             e1.printStackTrace();
  33.         }
  34.     }
  35.    
  36.     public static synchronized void openConnection()
  37.     {
  38.         try
  39.         {
  40.             connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/straf", "root", "Pidiboe002");
  41.         }
  42.        
  43.         catch (Exception e1)
  44.         {
  45.             e1.printStackTrace();
  46.         }
  47.     }
  48.    
  49.     public static synchronized void closeConnection()
  50.     {
  51.         try
  52.         {
  53.             connection.close();
  54.         }
  55.        
  56.         catch (Exception e1)
  57.         {
  58.             e1.printStackTrace();
  59.         }
  60.     }
  61.    
  62.     public static synchronized boolean playerdataContainsPlayer(String args)
  63.     {
  64.         openConnection();
  65.         try
  66.         {
  67.             PreparedStatement sql = (PreparedStatement) connection.prepareStatement("SELECT * FROM `player_data` WHERE player_name=?;");
  68.             sql.setString(1, args);
  69.            
  70.             ResultSet result = sql.executeQuery();
  71.             boolean containsPlayer = result.next();
  72.            
  73.             sql.close();
  74.             result.close();
  75.            
  76.             return containsPlayer;
  77.         }
  78.        
  79.         catch (Exception e1)
  80.         {
  81.             e1.printStackTrace();
  82.         }
  83.        
  84.         finally
  85.         {
  86.             closeConnection();
  87.         }
  88.         return true;
  89.     }
  90. }
Add Comment
Please, Sign In to add comment