diff --git a/src/main/java/io/github/simplexdev/simplexcore/sql/Database.java b/src/main/java/io/github/simplexdev/simplexcore/sql/Database.java new file mode 100644 index 0000000..a51ef56 --- /dev/null +++ b/src/main/java/io/github/simplexdev/simplexcore/sql/Database.java @@ -0,0 +1,92 @@ +package io.github.simplexdev.simplexcore.sql; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class Database { + + /* + * More to be added soon. + */ + + public static void createTable(String table, String columns) { + PreparedStatement ps; + try { + ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS " + + table + " (" + columns + ");"); + ps.executeUpdate(); + } catch(SQLException ex) { + // TODO + } + } + + public static void insertData(String columns, String values, String table) { + PreparedStatement ps; + try { + ps = MySQL.getConnection().prepareStatement("INSERT INTO " + + table + "(" + columns + ") VALUES (" + values + ");"); + ps.executeUpdate(); + } catch(SQLException ex) { + // TODO + } + } + + public static void deleteData(String table, String column, Object value) { + PreparedStatement ps; + try { + ps = MySQL.getConnection().prepareStatement("DELETE FROM " + table + " WHERE " + column + "=?"); + ps.setObject(1, value); + ps.executeUpdate(); + } catch(SQLException ex) { + // TODO + } + } + + public static void set(String table, String gate, Object gate_value, String column, Object value) { + PreparedStatement ps; + try { + ps = MySQL.getConnection().prepareStatement("UPDATE " + table + " SET " + column + "=? WHERE " + gate + "=?"); + ps.setObject(1, value); + ps.setObject(2, gate_value); + ps.executeUpdate(); + } catch(SQLException ex) { + // TODO + } + } + + public static boolean exists(String table, String column, Object value) { + PreparedStatement ps; + try { + ps = MySQL.getConnection().prepareStatement("SELECT * FROM " + + table + " WHERE " + column + "=?"); + ps.setObject(1, value); + + ResultSet results = ps.executeQuery(); + return results.next(); + } catch(SQLException ex) { + // TODO + } + return false; + } + + public static Object get(String table, String column, String gate, Object gate_value) { + PreparedStatement ps; + try { + ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table + + " WHERE " + gate + "=?"); + ps.setObject(1, gate_value); + + ResultSet rs = ps.executeQuery(); + Object toReturn; + + if (rs.next()) { + toReturn = rs.getObject("CODE"); + return toReturn; + } + } catch(SQLException ex) { + // TODO + } + return null; + } +} diff --git a/src/main/java/io/github/simplexdev/simplexcore/sql/MySQL.java b/src/main/java/io/github/simplexdev/simplexcore/sql/MySQL.java new file mode 100644 index 0000000..0d94036 --- /dev/null +++ b/src/main/java/io/github/simplexdev/simplexcore/sql/MySQL.java @@ -0,0 +1,55 @@ +package io.github.simplexdev.simplexcore.sql; + +import javax.annotation.Nullable; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MySQL { + + private static Connection connection; + + public static void setConnection(String host, String user, String pass, String database, @Nullable String port) { + if (port == null) port = "3306"; + if (host == null || user == null || pass == null) return; + + try { + connection = DriverManager.getConnection("jdbc:mysql://" + + host + ":" + port + "/" + database + "?autoReconnect=true&useSSL=false", user, pass); + // TODO SQL successfully connected message + } catch(SQLException ex) { + // TODO SQL couldn't connect message + } + } + + public static void connect(String host, String user, String pass, String database, @Nullable String port) { + if (!isConnected()) { + setConnection(host, user, pass, database, port); + } + } + + public static void disconnect() { + try { + if (isConnected()) { + connection.close(); + } + } catch(SQLException ex) { + // TODO Couldn't disconnect SQL connection message. (Possible Solutions: There's no sql connection (SQL is not connected)). + } + } + + public static boolean isConnected() { + if (connection != null) + try { + return !connection.isClosed(); + } catch (SQLException e) { + // TODO SQL Connection error message. + } + return false; + } + + public static Connection getConnection() { + return connection; + } + +} diff --git a/src/main/java/io/github/simplexdev/simplexcore/sql/SQLite.java b/src/main/java/io/github/simplexdev/simplexcore/sql/SQLite.java new file mode 100644 index 0000000..6821ff1 --- /dev/null +++ b/src/main/java/io/github/simplexdev/simplexcore/sql/SQLite.java @@ -0,0 +1,8 @@ +package io.github.simplexdev.simplexcore.sql; + +public class SQLite { + + /* + TODO + */ +}