-
Notifications
You must be signed in to change notification settings - Fork 0
DatabaseManager Class
This class allows the access and creation of databases.
Class declaration:
public final class DatabaseManager
Known subclasses: N/A(Since the class is final).
Superclass: java.lang.Object
Implemented Interfaces: none
public DatabaseManager()
This constructor instantiates a DatabaseManager instance.
public Database access(String path, String user, String password) throws Throwable
This method allows access for a Specific Database given the credentials(Username and Password)
Throws:
-
Throwable- When something went wrong
Returns: A io.github.demnetwork.sjdb.Database instance that represents the Database.
public void createDatabase(String user, String password, String path) throws IOException
This method creates a Database and saves it into a file.
Throws:
-
IOException- If the file already exists
Returns: N\A
No Public Inner Types.
Example: Accessing a Database
Maven/Classpath installation:
import io.github.demnetwork.sjdb.*
public class Main {
public static void main(String[] args) {
DatabaseManager mgr = new DatabaseManager();
Database db = null;
try {
db = mgr.access();
} catch (Throwable t) {
// Handle Exception here
}
if (db != null) {
// Do something with the Database
}
}
}Reflection installation
import java.lang.reflect.*
import java.net.*
import java.util.*
import java.io.*;
public class Main {
private static int bn;
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
}
//Load Classes
Class<?> databaseManagerClass = cl.loadClass("io.github.demnetwork.sjdb.DatabaseManager");
Class<?> dbClass = cl.loadClass("io.github.demnetwork.sjdb.Database");
//Create Objects
Object mgr = databaseManagerClass.getConstructor().newInstance();
Method mgrAccessMethod = databaseManager.getMethod("access", String.class, String.class, String.class);
Object db = mgrAccessMethod.invoke(mgr, "path/to/database.sdb", "username", "password");
//Do something with the db Object
}
}(Note: make sure to close the URLClassLoader after you no longer need to load classes)
SJDB Is licensed under the MIT License, and by using the SJDB API you agree to the license below:
Copyright (c) 2024 Arthur Yoshikasu Arakaki
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The license above states that you can use, modify, and distribute the software, even commercially, as long as the license above is included.