Loading and Saving Tutorial

Apr 1, 2018
Loading and Saving Tutorial
  • Loading and saving are the easiest part in using the SQLObjectConverter because they do not involve customizations and configurations.


    First, create an SQLObjectConverter object, providing a connection with database selected through the constructor. If any method inside the converter is called before selecting a database within the connection will cause a com.rcextract.minecord.sql.DatabaseAccessException.
    Code (Java):
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306?autoReconnect=true", "root", "admin");
    SQLObjectConverter converter = new SQLObjectConverter(connection);
    If you want to save an object, do saveObject(Object). This statement may cause some exceptions listed below:
    • When the connection to the database is expired, a java.sql.SQLTimeoutException will be thrown.
    • If any classes related to serialization do not meet the requirements, or an internal error occurs, a com.rcextract.minecord.sql.DataLoadException will be thrown.
    • Finally, other exceptions thrown from methods called within the serialization and outside of the SQLObjectConverter class will also be thrown as java.lang.Throwable. (The exception is obtained thrown InvocationTargetException.getCause()).
    Code (Java):
    This method will return a String and an Integer as a Pair object. The string represents the table name where the object is saved, and the integer represents the row in the table, which represents the serialized form of the object. These values can be used to load the object back.

    If you want to save multiple objects with the same type at the same time, you can do the following:
    Code (Java):
    converter.saveObjects(Arrays.asList(new String[] {"hi", "bye"}));

    First, obtain an SQLObjectConverter object. It can be done by using the previously created one or a new one with the same connection passed through the constructor. (An SQLObjectConverter can be used for saving or loading anytime. Fields inside the SQLObjectConverter are only caches loaded from the database to help loading data, and will be cleared once the called method type changed between serialization and deserialization.) Then, do loadObject(String, int, boolean).
    • The first argument is used to determine the row in which table you want to load. If the class of the object serialized and saved into the table implements the SQLObjectConverter API, the name will be the value() returned from the annotation com.rcextract.minecord.sql.SerializableAs annotated on the class. Otherwise, the name will be the simple name of the class. All characters within it will not be capitalized.
    • The second argument is used to determine which row you want to load in a table.
    • Finally, the third argument is used to determine whether the row should be deleted after the object is successfully loaded.
    Code (Java):
    converter.loadObject("string", 1, true);
    This method will return the loaded object.

    If you want to load all objects from a table, you can do the following:
    Code (Java):
    This method will return a list of objects which are saved in the table.

    Finally, if you want to load all objects which is assignable from a specific type, you can do the following:
    Code (Java):
    This method will return an array map, where the key is the map where the object is stored, and the value is the loaded object. The table name is provided because any table that represents object with supertype or subtype of the provided type will also be loaded.
  • Loading...
  • Loading...