Database - Storing information

Discussion in 'Plugin Development' started by YuzkoCode, Jul 7, 2013.

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

    YuzkoCode

    Hi, I created an sql database through cPanel X and was wondering how I would store a players name after they have mined diamonds ore;

    Code:java
    1.  
    2. public class sBlock implements Listener
    3. {
    4.  
    5. @EventHandler
    6. public void diamondBreak(BlockBreakEvent e)
    7. {
    8.  
    9. if(e.getBlock().getType() == Material.DIAMOND_ORE)
    10. {
    11. Player p = e.getPlayer();
    12.  
    13. e.getBlock().setType(Material.AIR);
    14. e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(Material.REDSTONE, 12));
    15. e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(Material.IRON_ORE, 6));
    16. e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(Material.GOLD_ORE, 6));
    17. e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(Material.COAL_ORE, 12));
    18. p.sendMessage(ChatColor.AQUA + "The diamond ore crumbled into other minerals");
    19.  
    20. //Store players name into database, so you can get a list of players names who have mined diamond ore at a later stage.
    21. }
    22.  
    23. }
    24.  
    25. }
     
  2. Offline

    JoshArgent

  3. Offline

    YuzkoCode

  4. Offline

    JoshArgent

    Yes you can although if you're just making one you might find it easier to do it through phpMyAdmin.
    If you have a read of this: http://www.w3schools.com/php/php_mysql_create.asp
    You can use w3schools for all of your MySQL syntax, they have some great examples.
     
  5. Offline

    YuzkoCode

    And do I do the creation of the table in whatever class?
     
  6. Offline

    JoshArgent

    Well you will have a copy of the MySQL class in the first tutorial I linked and then you will execute a mysql query to create a table. You can do that from where ever you created the MySQL class instance.
     
  7. Offline

    YuzkoCode

    Eh sorry im getting confused, what exactly do I put in the class MySQL.java to create a table? I want the table to have two parts - Playername & How many diamond ores theyve mined
     
  8. Offline

    JoshArgent

    Okay, follow this tutorial here: https://forums.bukkit.org/threads/tutorial-using-mysql-in-your-plugins.132309/
    Then to execute your own query (insert or create table) just change the text inside this line:
    Code:
    statement.executeUpdate("INSERT INTO tokens (`PlayerName`, `tokens`) VALUES ('" + name + "', '0');");
    System.out.println("Inserted info");
    
    You can put that line anywhere in your plugin so long as you have repeated the steps above.
     
Thread Status:
Not open for further replies.

Share This Page