Advertisement
Guest User

MySql

a guest
Oct 30th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package hu.tucsok007.killsound;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. import org.bukkit.entity.Player;
  8.  
  9. import hu.tucsok007.mysql.MySQL;
  10.  
  11. public class MySqlManager
  12. {
  13.     private final KillSound main;
  14.     private MySQL db;
  15.    
  16.     public MySqlManager(KillSound p)
  17.     {
  18.         this.main = p;
  19.     }
  20.    
  21.     public void setupDB() throws SQLException, ClassNotFoundException
  22.     {
  23.         this.db = new MySQL(this.main, "localhost", "3306", "killpoints", "root", "tBgk8zzX7StnBRm");
  24.         this.db.openConnection();
  25.         Statement statement = this.db.getConnection().createStatement();
  26.         statement.executeUpdate("CREATE TABLE IF NOT EXISTS `killpointsdata` (`Name` varchar(32),`Points` int)");
  27.         statement.close();
  28.     }
  29.    
  30.     public void closeDB() throws SQLException
  31.     {
  32.         this.db.closeConnection();
  33.     }
  34.    
  35.     public int getPoints(Player player) throws SQLException, ClassNotFoundException
  36.     {
  37.         String p = player.getName();
  38.         int playername = KillSound.pontok.get(p);
  39.         if(!this.db.checkConnection())
  40.         {
  41.             this.db.openConnection();
  42.         }
  43.         Statement statement = this.db.getConnection().createStatement();
  44.         ResultSet rs = statement.executeQuery("SELECT * FROM `killpointsdata` WHERE `Name`='"+playername+"';");
  45.         if(!rs.next())
  46.         {
  47.             return 0;
  48.         }
  49.         return rs.getInt("Points");
  50.     }
  51.    
  52.     public void updatePlayer(Player player) throws SQLException, ClassNotFoundException
  53.     {
  54.         String playername = player.getDisplayName().toLowerCase();
  55.         if(!this.db.checkConnection())
  56.         {
  57.             this.db.openConnection();
  58.         }
  59.         Statement statement = this.db.getConnection().createStatement();
  60.         int points = this.getPoints(player);
  61.         if(points!=0)
  62.         {
  63.             statement.executeUpdate("UPDATE `killpointsdata` SET `Points`='"+KillSound.pontok.get(playername)+"' WHERE `Name`='" + playername + "';");
  64.         }
  65.         else
  66.         {
  67.             statement.executeUpdate("INSERT INTO `killpointsdata` (`Name`,`Points`) VALUES ('" + playername + "','"+KillSound.pontok.get(playername)+"');");
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement