Inserting serialized objects?

Discussion in 'Plugin Development' started by Xp10d3, Jun 7, 2021.

  1. Offline

    Xp10d3

    Hello. So I'm a noob at serialization and want to insert a serialized object into a database, then get that serialized object and compare it to a list. That's the basic premise of my issue. By the way this is a Hippo Practice plugin, but since I bet no one understands what that is, this is basically it:

    The player will be placing a bunch of blocks (lets just assume infinite) in a specific pattern which I'll store in a list using BlockPlaceEvent. They can "save" a hippo, which will then serialize the list and then insert that list into a database (which I have issues with, since I'm not familiar with serialization). When the player "practices", I want to deserialize the list and compare that list to the current list (which stores every block the player places).

    Relavant code:
    Code:java
    1.  
    2. package eltik.endran.hippo;
    3.  
    4. import java.io.IOException;
    5. import java.i:confused:bjectInputStream;
    6. import java.io.Serializable;
    7. import java.util.List;
    8.  
    9. import org.bukkit.block.Block;
    10.  
    11. public class Serialize implements Serializable {
    12.  
    13. private static final long serialVersionUID = 1L; // ik i couldve just suppressed the warnings, but this was all just a test haha
    14.  
    15. public static String serializeObject(List<Block> list) {
    16. String longg = ""; // Yes I know I called the variable longg. Don't question lol.
    17. try {
    18. longg = objIn.readUTF();
    19. } catch (IOException e) {
    20. e.printStackTrace();
    21. }
    22. return longg;
    23. }
    24.  
    25. }
    26.  

    Code:java
    1.  
    2. public static void insertMap(final UUID uuid, final List<Block> list) {
    3. String finall = Serialize.serializeObject(list);
    4. try {
    5. PreparedStatement statement = plugin.getConnection()
    6. .prepareStatement("SELECT * FROM " + plugin.table + " WHERE UUID=?");
    7. statement.setString(1, uuid.toString());
    8. ResultSet results = statement.executeQuery();
    9. if (!results.next()) {
    10. PreparedStatement insert = plugin.getConnection()
    11. .prepareStatement("INSERT INTO " + plugin.table + " (UUID,BLOCK) VALUES (?)");
    12. insert.setString(1, uuid.toString());
    13. insert.setString(2, finall);
    14. insert.executeUpdate();
    15. results.close();
    16. Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Block Inserted"); // or some other message
    17.  
    18. } else {
    19. return;
    20. }
    21. } catch (SQLException e) {
    22. e.printStackTrace();
    23. }
    24. }
    25.  


    Full classes:
    HippoPractice.java: tJHQYQ6eAI | SourceBin
    PlayerListeners.java: Zhf3Wx1qKb | SourceBin
    Serialize.java: TipAv2qRZS | SourceBin
    Core.java: xrVYkyupZR | SourceBin
     
  2. Offline

    Kars

    I didn't look at your code much but the idea of serialization is to let Java handle it.
    1. Serialize object -> get a String;
    2. Do whatever with String (store in DB);
    3. De-serialize String -> get object;
     
  3. Offline

    Xp10d3

    Yeah, the issue for me is that I'm not entirely sure I'm doing the serialization part correctly. I understand how I want to use serialization as you stated, but as I said, I'm not too sure I did it correctly. Most specifically, the serializeObject method. Sorry if I'm asking a weird question. I'm sure that what I'm asking is confusing so I apologize for that.
     
  4. Online

    davidclue

  5. Offline

    Xp10d3

  6. Offline

    Kars

    Use Gson. It is easy.
    https://howtodoinjava.com/gson/gson-serialize-deserialize-json/
     

Share This Page