I need to COMPILE
This commit is contained in:
parent
3bf2b2394b
commit
216425e983
|
|
@ -1,15 +1,157 @@
|
||||||
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
import java.io.File;
|
||||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
import java.io.FileWriter;
|
||||||
public class CreeperSQL {
|
import java.io.IOException;
|
||||||
public static void main(String[] args) {
|
import java.nio.file.Files;
|
||||||
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
|
import java.nio.file.Paths;
|
||||||
// to see how IntelliJ IDEA suggests fixing it.
|
import java.sql.*;
|
||||||
System.out.printf("Hello and welcome!");
|
import java.util.*;
|
||||||
|
|
||||||
for (int i = 1; i <= 5; i++) {
|
public class CreeperSQL {
|
||||||
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
|
||||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
|
private String host;
|
||||||
System.out.println("i = " + i);
|
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<String> lines = Files.readAllLines(Paths.get(configFile.getPath()));
|
||||||
|
Map<String, String> 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
//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<String> getTables() {
|
||||||
|
List<String> 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<String> 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<List<String>> rows = new ArrayList<>();
|
||||||
|
public int affectedRows = 0;
|
||||||
|
public String error = null;
|
||||||
|
public boolean hasError() { return error != null; }
|
||||||
|
public boolean hasRows() { return !rows.isEmpty(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user