[Solved] Insert text in Database

Discussion in 'Plugin Development' started by NoMansLand, Jun 10, 2012.

  1. Offline

    NoMansLand

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hello,

    I am getting some weird stuff submitted into a database.

    This is the code (i've removed some of the other stuff)

    I have imported AdamKi11s Plugin:
    Code:
    import couk.Adamki11s.SQL.SyncSQL;
    
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
               
            Player player = null;
                    if  (sender instanceof Player) {
                        player = (Player) sender;
                    }
            if(cmd.getName().equalsIgnoreCase("support")) {
                if (player == null) {
                    sender.sendMessage("[ICU] This command must be run in game only.");
                } else {
                    if (args.length < 2) {
                        sender.sendMessage("§7[§5ICU§7] You must enter some more details or else the ticket will be ignored. Then you will be laughed at, ICU doesn't want to see this. ICU Loves you.");
                    } else {
                        final Random generator = new Random();
                        Object id = null;
                        @SuppressWarnings("null")
                        Date date = new Date((long) id);
                        SyncSQL sql = new SyncSQL("216.244.84.162","myfallen_ticket","xxxxx","xxxx");
                        sql.initialise();
                        sql.standardQuery("INSERT INTO `myfallen_ticket`.`tickets` (`id`, `sender`, `body`, `submit_date`, `ticketID`, `currentStatus`, `priority`, `assignedTo`, `title`) VALUES (NULL, '"+ player +  "', '"  +  args +  "', '" + date + "', 'MFT-" + generator + "', 'Open', 'Medium', 'Ingame Staff', 'Minecraft Support');");
                        sender.sendMessage("§7[§5ICU§7] Your ticket has been submitted. Click the following link:");
                        sender.sendMessage("§7[§5ICU§7] §bhttp://support.myfallen.net/ticket.php?id=MFT-" + generator + "");
                    }
                }
                return true;
            }
            return false;
        }
     
    }
     
    

    This post has been edited 2 times. It was last edited by NoMansLand Jun 10, 2012.
  2. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Line 44 has a null pointer... most likely where you suppress warnings of null at the Date() object :)
  3. Offline

    NoMansLand

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    With the data i'm getting sent through:

    CraftPlayer{name=NoMansLand_MFGC}
    [Ljava.lang.String;@3c7f9d04
    java.text.SimpleDateFormat@beefd189
    MFT-java.util.Random@5e1d90a3

    Is there any way to change this to show the data?

    Code:
     Date dNow = new Date(0 );
                        Format formatter;
                        Date date = new Date(0);
                        formatter = new SimpleDateFormat("dd/MM/yyyy 'at' HH.mm.ss");
                        Object s = null;
                        s = formatter.format(date);
                        Random randomGenerator = new Random();
                        int randomInt = randomGenerator.nextInt(999999);
                        SyncSQL sql = new SyncSQL("myfallen.net:3306","myfallen_ticket","xxxxxxxxxxxxxx","xxxxxxxxxxxx");
                        sql.initialise();
                        sql.standardQuery("INSERT INTO `myfallen_ticket`.`tickets` (`id`, `sender`, `body`, `submit_date`, `ticketID`, `currentStatus`, `priority`, `assignedTo`, `title`) VALUES (NULL, '"+ sender.getName() +  "', '"  +  args.toString() +  "', '" + s + "', 'MFT-" + randomInt + "', 'Open', 'Medium', 'Ingame Staff', 'Minecraft Support');");                    sender.sendMessage("§7[§5ICU§7] Your ticket has been submitted. Click the following link:");
                        sender.sendMessage("§7[§5ICU§7] §bhttp://support.myfallen.net/ticket.php?id=MFT-" + randomInt + "");
                    }
                }
                return true;
    
    Fixed the getName()

    This post has been edited 4 times. It was last edited by NoMansLand Jun 10, 2012.
  4. Offline

    NoMansLand

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Fixed Random Number.

    Just need to do date & the text.
  5. Offline

    NoMansLand

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    How can I get all the text from the args[]? with out having to save to the database again & again & again
  6. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You mean something like "/msg arg1 And long message that holds more arguments" ?

    Then you merge them back together... I'm unsure if there's a single method for this but you can do it like this as well:
    Code:
    String message = "";
    
    // 1 is the argument to start the message from
    for(int i = 1; i < args.length; i++)
    {
    	message = message + " " + args[i];
    }
    
    // message now holds all arguments since arg 1 (2nd argument)
  7. Offline

    NoMansLand

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Your a legend!

    I spent hours looking for this and it was so simple!! Thank you so much!!
  8. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Oh, actually an improvement to avoid starting space:
    Code:
    int startArg = 1; // 2nd arg
    String message = "";
     
    for(int i = startArg; i < args.length; i++)
    {
        message = message + (i == startArg ? "" : " ") + args[i];
    }
     
    // "message" now holds all arguments since arg defined in startArg

    This post has been edited 2 times. It was last edited by Digi Jun 10, 2012.
  9. Offline

    Mitsugaru

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    If I could suggest one change: use StringBuilder.
    Code:
    final StringBuilder sb = new StringBuilder();
    for(int i = startArg; i < args.length; i++)
    {
        sb.append(args[i] + " ");
    }
    final String message = sb.toString();
    Or something like the above.

    This post has been edited 1 time. It was last edited by Mitsugaru Jun 10, 2012.
  10. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hmm, yes, I just googled about those to know the diferences and it seems that re-asigning a string actually creates a new one and it discards the old one for GC, which isn't favorable for memory.

    It goes down to:
    From: http://javarevisited.blogspot.ro/2011/07/string-vs-stringbuffer-vs-stringbuilder.html

    This post has been edited 1 time. It was last edited by Digi Jun 10, 2012.

Share This Page