Solved Trouble with classes

Discussion in 'Spigot Plugin Development' started by Illya2004, Jun 12, 2021.

  1. Hello guys, i have a problem. I have 2 classes, and i need take from Commands class in method onCommand variable nameS, and put it in class DB in method getMoney.
    DB
    Code (Java):
    package money.main;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import org.bukkit.entity.Player;

    import java.sql.SQLException;

    public class DB {
       
        private Money plugin;
        private String host;
        private String login;
        private String password;
        private String sender;

        public DB(String host, String login, String password, Money plugin) {
            this.host = host;
            this.login = login;
            this.password = password;
            this.plugin = plugin;
        }

        public Connection getConnection() throws Exception {
            return DriverManager.getConnection(this.host,this.login,this.password);              
        }


        public int GetMoney(String sender) throws Exception {
            this.sender = sender;
            String tableName = this.plugin.getConfig().getString("tableName");
            String dbNames = this.plugin.getConfig().getString("columnFirst");
            String dbBalance = this.plugin.getConfig().getString("columnSecond");
            Connection c = this.getConnection();
            Statement s = c.createStatement();
            ResultSet  res = s.executeQuery("SELECT " + dbBalance +" FROM "+ tableName +" WHERE " + dbNames +" = '"+ sender +"'" );
            res.next();
            return res.getInt(dbBalance);
           
               
           
        }
    Commands
    Code (Java):
    package money.main;

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;

    public class Commands implements CommandExecutor {
       


        private Money plugin;
       
        public static  String nameS;
        private DB database;


        public Commands(Money plugin) {
           
            this.plugin = plugin;
            String host = this.plugin.getConfig().getString("host");
            String login = this.plugin.getConfig().getString("login");
            String password = this.plugin.getConfig().getString("password");
            this.database = new DB(host, login, password, plugin);

        }


        @Override
        public  boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
           
               
            String nameS = sender.getName();

            if(args.length == 0 ) {
               
                String showBalance = this.plugin.getConfig().getString("messages.showBalance");  
               
                try {
                    showBalance = showBalance.replace("&", "\u00a7").replace("_nm", nameS).replace("_bl", String.valueOf(this.database.GetMoney(nameS)));
                    sender.sendMessage(showBalance);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            }
    return true;
    }
    How i can do this?
     
    #1 Illya2004, Jun 12, 2021
    Last edited by a moderator: Jun 12, 2021
  2. You could do something like this:
    Code (Java):
    public Commands(Money plugin, DB database) {
            this.plugin = plugin;
            this.database = database;
    }
    I might be a bit confused on what you're trying to do though
     
  3. Hi, sorry but I don't get the problem. You already got the sender in the GetMoney method, right?
     
  4. I need to get sender in class GetMoney, i take sender just in onCommand
     
  5. I still don't understand, you already got this.sender = sender;
     
  6. Yeah but you pass it over as a parameter, your code looks fine, why do you save the sender variable inside db though? This can cause problems.
     
  7. Oh sorry, this is my old code

    NEW DB
    package money.main;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import org.bukkit.entity.Player;

    import java.sql.SQLException;

    public class DB {

    private Money plugin;
    private String host;
    private String login;
    private String password;
    private String sender;

    public DB(String host, String login, String password, Money plugin) {
    this.host = host;
    this.login = login;
    this.password = password;
    this.plugin = plugin;
    }

    public Connection getConnection() throws Exception {
    return DriverManager.getConnection(this.host,this.login,this.password);
    }


    public int GetMoney(String name) throws Exception {

    String tableName = this.plugin.getConfig().getString("tableName");
    String dbNames = this.plugin.getConfig().getString("columnFirst");
    String dbBalance = this.plugin.getConfig().getString("columnSecond");
    Connection c = this.getConnection();
    Statement s = c.createStatement();
    ResultSet res = s.executeQuery("SELECT " + dbBalance +" FROM "+ tableName +" WHERE " + dbNames +" = '"+ name +"'" );
    res.next();
    return res.getInt(dbBalance);



    }
     
  8. Ok but what isn’t your working??? This should work if the database connection is successful, what is the issue / error?
     
  9. I still don't get what's wrong so unfortunately I can't help you :c
     
  10. Yes, java.sql.SQLException: Illegal operation on empty result set.
     
  11. Something in the select request isn’t right, no result is returned. Either the player isn’t in the table or column doesn’t exist
     
    • Like Like x 1
  12. Oh my god, yes u are rite, Either the player isn’t in the table. Shit thanks for help.