Advertisement
Greyhunter99

#94

Apr 21st, 2018
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package me.ghg.data;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10.  
  11. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  12.  
  13. public class Database extends JavaPlugin {
  14.  
  15.     Connection connection;
  16.     String host, database, username, password;
  17.     int port;
  18.    
  19.     @Override
  20.     public void onEnable(){
  21.         try {
  22.             connect();
  23.         } catch (ClassNotFoundException e) {
  24.             e.printStackTrace();
  25.         } catch (SQLException e) {
  26.             e.printStackTrace();
  27.         }
  28.     }
  29.    
  30.     public void connect() throws ClassNotFoundException, SQLException {
  31.         Class.forName("com.mysql.jdbc.Driver");
  32.         MysqlDataSource dataSource = new MysqlDataSource();
  33.         dataSource.setServerName(host);
  34.         dataSource.setPort(port);
  35.         dataSource.setDatabaseName(database);
  36.         dataSource.setUser(username);
  37.         dataSource.setPassword(password);  
  38.         connection = dataSource.getConnection();
  39.     }
  40.    
  41.     public void addScore(Player player, int score) throws SQLException{
  42.         PreparedStatement stat = connection.prepareStatement("INSERT INTO PlayerScore(Player_Name,Score) VALUES (?,?)");
  43.         stat.setString(1, player.getName());
  44.         stat.setInt(2, score);
  45.         stat.executeQuery();
  46.     }
  47.    
  48.     public void setScore(Player player, int score) throws SQLException{
  49.         PreparedStatement stat = connection.prepareStatement("UPDATE PlayerScores SET Score = ? WHERE Player_Name = ?");
  50.         stat.setString(2, player.getName());
  51.         stat.setInt(1, score);
  52.         stat.executeQuery();
  53.     }
  54.    
  55.     public int getScore(Player player) throws SQLException{
  56.         int score = 0;
  57.         PreparedStatement stat = connection.prepareStatement("SELECT Score FROM PlayerScores WHERE Player_Name = ?");
  58.         stat.setString(1, player.getName());
  59.         ResultSet result = stat.executeQuery();
  60.         while(result.next()){
  61.             score = result.getInt("Score");
  62.         }
  63.         return score;
  64.     }
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement