Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQLCoins {
- private String url_base, host, name, username, password, table;
- private Connection connection;
- public MySQLCoins(String url_base, String host, String name, String username, String password, String table){
- this.url_base = url_base;
- this.host = host;
- this.name = name;
- this.username = username;
- this.password = password;
- this.table = table;
- }
- public void connection(){
- if(!isConnected()){
- try{
- connection = DriverManager.getConnection(url_base+host+"/"+name, username, password);
- }catch (SQLException e){
- Bukkit.broadcastMessage(e.getMessage());
- }
- }
- }
- public void deconnection(){
- if(isConnected()){
- try{
- connection.close();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- }
- private boolean isConnected(){
- try{
- if((connection == null)||(connection.isClosed())||(!connection.isValid(5))){
- return false;
- }else{
- return true;
- }
- }catch (SQLException e){
- e.printStackTrace();
- }
- return false;
- }
- private Connection getConnection(){
- return connection;
- }
- public void createAccount(Player p){
- try{
- PreparedStatement sts = getConnection().prepareStatement("SELECT coins FROM "+table+" WHERE uuid = ?");
- sts.setString(1, p.getUniqueId().toString());
- ResultSet rs = sts.executeQuery();
- if(!rs.next()){
- sts.close();
- PreparedStatement sts2 = getConnection().prepareStatement("INSERT INTO "+table+" (uuid, coins) VALUES (?, ?)");
- sts2.setString(1, p.getUniqueId().toString());
- sts2.setInt(2, 10);
- sts2.executeUpdate();
- sts2.close();
- }
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public void addCoins(String uuid, int coins){
- try{
- PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET coins = coins + ? WHERE uuid = ?");
- sts.setInt(1, coins);
- sts.setString(2, uuid);
- sts.executeUpdate();
- sts.close();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public void removeCoins(String uuid, int coins){
- try{
- PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET coins = coins - ? WHERE uuid = ?");
- sts.setInt(1, coins);
- sts.setString(2, uuid);
- sts.executeUpdate();
- sts.close();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public Integer getCoins(String uuid){
- int coins = 0;
- try {
- PreparedStatement sts = getConnection().prepareStatement("SELECT coins FROM "+table+" WHERE uuid = ?");
- sts.setString(1, uuid);
- ResultSet rs = sts.executeQuery();
- if(!rs.next()){
- return coins;
- }
- coins = rs.getInt("coins");
- sts.close();
- }catch (SQLException e){
- Bukkit.broadcastMessage(e.getMessage());
- }
- return coins;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement