diff --git a/src/CreeperSQL.java b/src/CreeperSQL.java index c8bdecd..4c4a265 100644 --- a/src/CreeperSQL.java +++ b/src/CreeperSQL.java @@ -1,15 +1,157 @@ -//TIP To Run code, press or -// click the icon in the gutter. -public class CreeperSQL { - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.sql.*; +import java.util.*; - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); +public class CreeperSQL { + + private String host; + private String user; + private String pass; + private String database; + + private Connection connection; + + //config + + // call this or mcconfig before main who will even read thse docs + public void setConfig(String host, String user, String password, String database) { + this.host = host; + this.user = user; + this.pass = password; + this.database = database; + } + + public void readMinecraftConfig() throws IllegalStateException { + try { + File pluginFolder = new File("plugins/MySQLConnection"); + if (!pluginFolder.exists()) pluginFolder.mkdirs(); + + File configFile = new File(pluginFolder, "config.yml"); + + if (!configFile.exists()) { + configFile.createNewFile(); + String defaultConfig = "host: \nuser: \npassword: \ndatabase: "; + FileWriter writer = new FileWriter(configFile); + writer.write(defaultConfig); + writer.close(); + System.out.println("[CreeperSQL] config.yml created. Please fill in credentials before connecting!"); + } + + List lines = Files.readAllLines(Paths.get(configFile.getPath())); + Map config = new HashMap<>(); + for (String line : lines) { + if (!line.contains(":")) continue; + String[] parts = line.split(":", 2); + config.put(parts[0].trim(), parts[1].trim()); + } + + host = config.get("host"); + user = config.get("user"); + pass = config.get("password"); + database = config.get("database"); + + if (host == null || host.isEmpty() || + user == null || user.isEmpty() || + pass == null || pass.isEmpty() || + database == null || database.isEmpty()) { + throw new IllegalStateException("[CreeperSQL] Config incomplete! All values must be set before connecting."); + } + + System.out.println("[CreeperSQL] Config loaded successfully!"); + + } catch (IOException e) { + throw new IllegalStateException("[CreeperSQL] Failed to read config.yml: " + e.getMessage()); } } -} \ No newline at end of file + + //connect + + /** Connect using previously set config (host, user, pass, database) */ + public void connect() throws IllegalStateException { + if (host == null || user == null || pass == null || database == null) { + throw new IllegalStateException("[CreeperSQL] Config not set! Call setConfig() or readMinecraftConfig() first."); + } + openConnection(); + } + + private boolean openConnection() { + try { + if (connection != null && !connection.isClosed()) return true; + + String url = "jdbc:mysql://" + host + "/" + database + + "?useSSL=false&autoReconnect=true&characterEncoding=utf8"; + + connection = DriverManager.getConnection(url, user, pass); + return true; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + + public List getTables() { + List tables = new ArrayList<>(); + if (!openConnection()) return tables; + + try { + DatabaseMetaData meta = connection.getMetaData(); + ResultSet rs = meta.getTables(database, null, "%", new String[]{"TABLE"}); + while (rs.next()) tables.add(rs.getString("TABLE_NAME")); + rs.close(); + } catch (SQLException e) { e.printStackTrace(); } + + return tables; + } + + public QueryResult sqlAction(String sql) { + QueryResult result = new QueryResult(); + if (!openConnection()) return result; + + try { + Statement stmt = connection.createStatement(); + boolean hasResultSet = stmt.execute(sql); + + if (hasResultSet) { + ResultSet rs = stmt.getResultSet(); + ResultSetMetaData meta = rs.getMetaData(); + int columns = meta.getColumnCount(); + while (rs.next()) { + List row = new ArrayList<>(); + for (int i = 1; i <= columns; i++) row.add(rs.getString(i)); + result.rows.add(row); + } + rs.close(); + } else { + result.affectedRows = stmt.getUpdateCount(); + } + + stmt.close(); + } catch (SQLException e) { + result.error = e.getMessage(); + e.printStackTrace(); + } + + return result; + } + + public boolean createTable(String sql) { return sqlAction(sql).error == null; } + public boolean dropTable(String tableName) { return sqlAction("DROP TABLE IF EXISTS " + tableName + ";").error == null; } + public boolean insert(String sql) { return sqlAction(sql).affectedRows > 0; } + public boolean update(String sql) { return sqlAction(sql).affectedRows > 0; } + public boolean delete(String sql) { return sqlAction(sql).affectedRows > 0; } + + public void close() { try { if (connection != null) connection.close(); } catch (SQLException ignored) {} } + + public static class QueryResult { + public List> rows = new ArrayList<>(); + public int affectedRows = 0; + public String error = null; + public boolean hasError() { return error != null; } + public boolean hasRows() { return !rows.isEmpty(); } + } +}