136 lines
4.2 KiB
Java
136 lines
4.2 KiB
Java
package de.jatitv.commandguiv2.Spigot.system.database;
|
|
|
|
import de.jatitv.commandguiv2.Spigot.Main;
|
|
import de.jatitv.commandguiv2.Util;
|
|
import net.t2code.lib.Spigot.Lib.messages.send;
|
|
import org.bukkit.Bukkit;
|
|
|
|
import java.io.File;
|
|
import java.sql.*;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
public class SQLITE {
|
|
protected static String url;
|
|
private static Connection con;
|
|
|
|
public static void main() throws SQLException {
|
|
long long_ = System.currentTimeMillis();
|
|
File directory = new File(Main.getPath() + "/Storage");
|
|
if (!directory.exists()) {
|
|
directory.mkdir();
|
|
}
|
|
url = "jdbc:sqlite:" + Main.plugin.getDataFolder() + "/Storage/data.db";
|
|
con = DriverManager.getConnection(url);
|
|
Bukkit.getConsoleSender().sendMessage(Util.getPrefix() + " §2SQLITE successfully connected." + " §7- §e" + (System.currentTimeMillis() - long_) + "ms");
|
|
DatabaseMetaData dbmd = con.getMetaData();
|
|
}
|
|
|
|
public static void query(String query) {
|
|
|
|
send.debug(Main.plugin, query);
|
|
try {
|
|
Statement stmt = con.createStatement();
|
|
stmt.execute(query);
|
|
stmt.close();
|
|
} catch (SQLException e) {
|
|
System.err.println(e.getMessage());
|
|
}
|
|
}
|
|
|
|
public static void query(ArrayList<String> queryList) {
|
|
try {
|
|
Statement stmt = con.createStatement();
|
|
for (String query : queryList) {
|
|
send.debug(Main.plugin, query);
|
|
stmt.addBatch(query);
|
|
}
|
|
stmt.executeBatch();
|
|
stmt.close();
|
|
} catch (SQLException e) {
|
|
System.err.println(e.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
public static HashMap<String, ArrayList<String>> selectAll(String query) {
|
|
send.debug(Main.plugin, query);
|
|
HashMap<String, ArrayList<String>> Result = new HashMap<>();
|
|
try {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
int columns = rs.getMetaData().getColumnCount();
|
|
while (rs.next()) {
|
|
ArrayList<String> columnList = new ArrayList<>();
|
|
for (int i = 1; i <= columns; i++) {
|
|
columnList.add(rs.getString(i));
|
|
}
|
|
Result.put(rs.getString(1), columnList);
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
public static String select(String query) {
|
|
send.debug(Main.plugin, query);
|
|
String Ausgabe = "";
|
|
try {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
while (rs.next()) {
|
|
Ausgabe = String.valueOf(rs.getString(1));
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return Ausgabe;
|
|
}
|
|
|
|
public static int count(String query) {
|
|
send.debug(Main.plugin, query);
|
|
int count = 0;
|
|
try {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
while (rs.next()) {
|
|
count++;
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public static ArrayList<String> selectRow(String query) {
|
|
send.debug(Main.plugin, query);
|
|
ArrayList<String> Result = new ArrayList<>();
|
|
try {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
int columns = rs.getMetaData().getColumnCount();
|
|
while (rs.next()) {
|
|
for (int i = 1; i <= columns; i++) {
|
|
Result.add(rs.getString(i));
|
|
}
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
public static void close() throws SQLException {
|
|
if (con != null) con.close();
|
|
}
|
|
}
|