1.8.8 HashMap save in MySQL

Discussion in 'Spigot Plugin Development' started by DasChaos44, Mar 17, 2020.

  1. Hello,

    how can i save a hashmap for my Daily Reward System in a MySQL database?

    Is the HashMap an int??
  2. Store the hashmaps key/value and recreate it when required
  3. Depends on the structure of the map. If it's a set type (i.e. doesn't rely on user input), and is relatively small, you may want to have individual columns which correspond to each key. Otherwise, serializing the map into JSON (or any other similar data format) may be preferable.
  4. No! If you do this you don't need to use a database in the first place and can just dump it directly into a file.

    For this your database needs only one table with two columns (key and value) and the id column ofcourse. Then just go through all keys in your HashMap and save them with their corresponding value in the DB.
  5. HashMaps are pretty much the same things as a relational database...

    The HashMap will use a key to identify a value. A relational database will more than often use what is called a primary key to identify one or more values you call upon when reading the record.

    So like @derteufelqwe said, you can just dump it into file and read it once your plugin loads up again, do note though that if something unexpected suddenly crashes your server and doesn't give your plugin the proper time to shutdown it can cause the file to become corrupted in some cases or may just end up in the loss of some data.

    (EDIT: This can be safeguarded slightly by using a timer to save the file automatically, and then attempting to save the file upon deinitialization. This can still cause corruption if it's in the middle of writing to the file and it suddenly shuts down due to an unexpected error.)

    If you want to use a database you should use the database natively instead, it will work as a form of redundancy if the plugin fails since it reads and writes the second there are any changes, and there's bound to be less problems you will run into due to most relational database softwares such as MySQL or MariaDB actually tend to automatically backup your databases. So if anything goes wrong you can easily revert back an hour or a day before.

    Now using a relational database such as MySQL will give you the benefit of being able to possibly integrate it with other solutions, just like if you wanted to create a web panel that could display the data you're creating on a website. This is commonly used by more recent plugins like punishment systems and LuckyPerms too.
    #5 DustinThePlugin, Mar 17, 2020
    Last edited: Mar 17, 2020