[solved]JBDC: Select statement not returning int

Discussion in 'Plugin Development' started by herghost, Nov 28, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    herghost

    Hi all,

    My code is as follows:

    Code:
    package me.herghost.Fiery.commands;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Logger;
    
    import me.herghost.Fiery.util.Configuration;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class moneyCommand implements CommandExecutor
    {
    
    	Logger log = Logger.getLogger("Minecraft");
    
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    	{
    		readCommand((Player) sender, commandLabel, args);
    		return true;
    	}
    	private void readCommand(Player player, String cmd, String[] args )
    	{
    		if(cmd.equalsIgnoreCase("money")&& player instanceof Player)
    		{
    				Player p = (Player) player;
    				try
    					{
    						String user = Configuration.getString("settings.mysql.user");
    						String pass = Configuration.getString("settings.mysql.pass");
    						String url = "jdbc:mysql://localhost:3306/Fiery";
    						Connection conn = DriverManager.getConnection(url, user, pass);
    						Statement select = conn.createStatement();
    						ResultSet rs = select.executeQuery("SELECT balance FROM money WHERE p_name ='" + player + "'");
    						while (rs.next())
    						{
    							int balance = rs.getInt("balance");
    							p.sendMessage("Your current balance is '" + balance + "'");
    						}
    					}
    				catch
    				(SQLException e1)
    					{
    						e1.printStackTrace();
    					}
    		}
    	}
    }
    
    What this should do is simply return the balance of a user, however when the command /money is fired nothing happens, literally nothing! Nothing on screen, nothing in console.

    I have checked my database and the table is correctly filled and the field is an int, As I am testing locally there is only me in the db.

    Any ideas? The sql connection is correct as I can break it by running (!rs.next())
     
  2. Offline

    vildaberper

    Did you register your command in plugin.yml?
     
  3. Offline

    herghost

    Yes,

    this.getCommand("money").setExecutor(new moneyCommand());
     
  4. Offline

    herghost

    Got it! 'player' returns Craftbukkit, needed to be changed to p.getName();
     
Thread Status:
Not open for further replies.

Share This Page