MYSQL

Discussion in 'Spigot Plugin Development' started by Koekjeee, May 15, 2016.

  1. Whats wrong with this? No error but he doesn't send a broadcast message too?
    Code (Text):

    package me.koekjeee.stats;

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

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.koekjeee.mysql.MySQL;

    public class Main extends JavaPlugin {

       private Plugin plugin;
       
       MySQL MySQL = new MySQL("localhost", "3306", "plugin", "root", "");
      Connection c = null;
       
       public void onEnable() {
        try {
           c = MySQL.openConnection();
         } catch (ClassNotFoundException e) {
           e.printStackTrace();
         } catch (SQLException e) {
           e.printStackTrace();
         }
       }
       
       public boolean onCommand(CommandSender sender, Command cmd, String[] args) {
         if(cmd.getName().equalsIgnoreCase("get")) {
           try {
             Statement statement;
             statement = c.createStatement();
            ResultSet res = statement.executeQuery("SELECT * FROM test WHERE naam = '" + sender.getName() + "';");
            res.next();
             
             
            if(res.getString("naam") != sender.getName()) {
               Bukkit.broadcastMessage("Niks gevonden");
               statement.executeUpdate("INSERT INTO test (`naam`) VALUES ('" + sender.getName() + "');");
            } else {
               Bukkit.broadcastMessage(res.getString("naam"));
            }
           } catch (SQLException e) {
             e.printStackTrace();
           }
         }
         return false;
       }
    }
     
    I use this:
    https://bukkit.org/threads/using-mysql-in-your-plugins.132309/
     
  2. Do you get any errors in the console?
     
  3. Nope, i tried to do a sender.sendmessage on the beginning of the try { but i didn't received a message.
     
  4. @Koekjeee
    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String[] args) {
    You forgot the label argument, so it's not overriding
    Put:
    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Note: I recommend putting @ Override above a methof that you want to override, if you do something wrong you will get an error
     
  5. http://imgur.com/yJsVjO4
     
  6. @Koekjeee "Illegal operation on empty result set"
    There are no elements in the table with that name.
    Check if res.next() returns true, then do your stuff. If it returns false, no value by that name is found
     
  7. You must check if the result set has any inputted values into it or not before retrieving values from the result set. You must utilize #resultset.next(). The proper usage for this would be as follows.

    Code (Text):

    if (resultset.next())
    {
         ...  //retrieve values from result set
    }
    else
    {
         ...  //result set returned false, result set is empty or is not existant.
    }
     
     
  8. I have this now:
    Code (Text):

    package me.koekjeee.stats;

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

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.koekjeee.mysql.MySQL;

    public class Main extends JavaPlugin {

       private Plugin plugin;
       
       MySQL MySQL = new MySQL("localhost", "3306", "plugin", "root", "");
      Connection c = null;
       
       public void onEnable() {
        try {
           c = MySQL.openConnection();
         } catch (ClassNotFoundException e) {
           e.printStackTrace();
         } catch (SQLException e) {
           e.printStackTrace();
         }
       }
       
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         if(cmd.getName().equalsIgnoreCase("get")) {
           try {
             Statement statement;
             statement = c.createStatement();
            ResultSet res = statement.executeQuery("SELECT * FROM test WHERE naam = '" + sender.getName() + "';");
            res.next();
             
             if (res.next())
             {
               Bukkit.broadcastMessage(res.getString("naam"));
             }
             else
             {
               Bukkit.broadcastMessage("Niks gevonden");
               statement.executeUpdate("INSERT INTO test (`naam`) VALUES ('" + sender.getName() + "');");
             }
           } catch (SQLException e) {
             e.printStackTrace();
           }
         }
         return false;
       }
    }
     
    He says niks gevonden what means nothing found. But he don't insert the value sender.getname()?
    Code (Text):

     statement.executeUpdate("INSERT INTO test (`naam`) VALUES ('" + sender.getName() + "');");
    [/code
     
  9. @Koekjeee never execute multiple queries on a single statement.

    http://alvinalexander.com/java/java-mysql-update-query-example
    Code (Java):

    // create the java mysql update preparedstatement
     String query = "update users set num_points = ? where first_name = ?";
     PreparedStatement preparedStmt = conn.prepareStatement(query);
     preparedStmt.setInt  (1, 6000);
     preparedStmt.setString(2, "Fred");

     // execute the java preparedstatement
     preparedStmt.executeUpdate();
     
  10. THX!