Skip to content
Arthur Yoshikasu Arakaki edited this page Mar 9, 2025 · 7 revisions

SJDB Wiki

Welcome to the SJDB wiki!

This Wiki will guide you into how to use SJDB.

Required acknowledgements:

  • Basic Java: You'll need to learn Java to use the SJDB API.

  • Java Reflection(Optional): Learn this if you need to do advanced operations with the SJDB API.

If you need help installing SJDB click here

If you only need to use the SJDB API you should go with Maven. If you don't have Maven, you should either get Maven or use Classpath method, but if you really need Maximum Flexibility and Dynamic loading, use Reflection.

Writing your first Program using SJDB

Maven/Classpath

This tutorial applies if you installed SJDB via Classpath or Maven methods

Creating and accessing a Database:

import io.github.demnetwork.sjdb.*

public class Main {
    public static void main(String[] args) {
        DatabaseManager mgr = new DatabaseManager(); // Creating a new Instance of DatabaseManager
        try {
            mgr.createDatabase("username", "password", "path/to/newDatabase.sdb"); // Creating a new Database
        } catch (IOException e) {
            System.out.println("Unable to create Database: ");
            e.printStackTrace();
        }
        Database db = null;
        try {
            db = mgr.access("path/to/newDatabase.sdb", "username", "password");
        } catch (Throwable t) {
            System.out.println("Unable to access the Database");
            t.printStackTrace();
        }
        if (db != null) {
            System.out.println("\'Database.getRoot().toString()\' output: " + db.getRoot().toString());
        }
    }
}

Don't worry if you don't know what this does right now, I'll explain you what this does later.

Reflection

This tutorial applies if you installed SJDB via Reflection method

import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;

public class Main {

        private static int bn = 0;

        public static void main(String[] args) throws Exception {
                File f = new File("./SJDB/sjdb.jar"); // Get File Object
                if (!f.exists()) {
                    throw new Error("Cannot find SJDB jar file"); // Throw error if file is not found
                }
                URLClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() }); // Create a class loader
                Class<?> runtimeClass = cl.loadClass("io.github.demnetwork.sjdb.internal.SJDBRuntime"); // Get the SJDBRuntime class
                bn = ((Integer) runtimeClass.getMethod("getBuildNumber").invoke(null)).intValue(); // Get the Build Number from SJDBRuntime
                if (bn != 1) {
                    cl.close(); // Close URLClassLoader
                    throw new Error("Incompatible SJDB Version."); // Throw error due to incompatible version
                }
                Class<?> databaseManagerClass = cl.loadClass("io.github.demnetwork.sjdb.DatabaseManager");
                Object databaseMgr = databaseManagerClass.getConstructor().newInstance();
                Method dbCreate = databaseManagerClass.getMethod("createDatabase", String.class, String.class, String.class);
                dbCreate.invoke(databaseMgr, "username", "password", "path/to/newDtabase.sdb");
                Method dbAccess = databaseManagerClass.getMethod("access", String.class, String.class, String.class);
                Object db = dbAccess.invoke("path/to/newDatabase.sdb", "username", "password");
                Class<?> dbClass = cl.loadClass("io.github.demnetwork.sjdb.Database");
                Class<?> dbRootClass = cl.loadClass("io.github.demnetwork.sjdb.dbelements.DBRootElement");
                Object root = dbClass.getMethod("getRoot").invoke();
                System.out.println("\'Database.getRoot().toString()\' result: " + root.toString());
        }
}

Don't worry if you don't know how the SJDB API works, I'll explain that later.

Clone this wiki locally