Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.ghg.data;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
- public class Database extends JavaPlugin {
- Connection connection;
- String host, database, username, password;
- int port;
- @Override
- public void onEnable(){
- try {
- connect();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void connect() throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.jdbc.Driver");
- MysqlDataSource dataSource = new MysqlDataSource();
- dataSource.setServerName(host);
- dataSource.setPort(port);
- dataSource.setDatabaseName(database);
- dataSource.setUser(username);
- dataSource.setPassword(password);
- connection = dataSource.getConnection();
- }
- public void addScore(Player player, int score) throws SQLException{
- PreparedStatement stat = connection.prepareStatement("INSERT INTO PlayerScore(Player_Name,Score) VALUES (?,?)");
- stat.setString(1, player.getName());
- stat.setInt(2, score);
- stat.executeQuery();
- }
- public void setScore(Player player, int score) throws SQLException{
- PreparedStatement stat = connection.prepareStatement("UPDATE PlayerScores SET Score = ? WHERE Player_Name = ?");
- stat.setString(2, player.getName());
- stat.setInt(1, score);
- stat.executeQuery();
- }
- public int getScore(Player player) throws SQLException{
- int score = 0;
- PreparedStatement stat = connection.prepareStatement("SELECT Score FROM PlayerScores WHERE Player_Name = ?");
- stat.setString(1, player.getName());
- ResultSet result = stat.executeQuery();
- while(result.next()){
- score = result.getInt("Score");
- }
- return score;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement