mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Started SQLite work
This commit is contained in:
1 parent
d3a787be67
commit
559e38fc4b
21 files changed
+788
-65
No files matched your search
@@ -0,0 +1,146 @@
|
||||
package me.william278.huskhomes2.Data.SQLite;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import me.william278.huskhomes2.Main;
|
||||
import me.william278.huskhomes2.Data.SQLite.Errors.Error;
|
||||
import me.william278.huskhomes2.Data.SQLite.Errors.Errors;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class Database {
|
||||
Main plugin;
|
||||
Connection connection;
|
||||
// The name of the table we created back in SQLite class.
|
||||
public String table = "table_name";
|
||||
public int tokens = 0;
|
||||
public Database(Main instance){
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
public abstract Connection getSQLConnection();
|
||||
|
||||
public abstract void load();
|
||||
|
||||
public void initialize(){
|
||||
connection = getSQLConnection();
|
||||
try{
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE player = ?");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
close(ps,rs);
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to retreive connection", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// These are the methods you can use to get things out of your database. You of course can make new ones to return different things in the database.
|
||||
// This returns the number of people the player killed.
|
||||
public Integer getTokens(String string) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '"+string+"';");
|
||||
|
||||
rs = ps.executeQuery();
|
||||
while(rs.next()){
|
||||
if(rs.getString("player").equalsIgnoreCase(string.toLowerCase())){ // Tell database to search for the player you sent into the method. e.g getTokens(sam) It will look for sam.
|
||||
return rs.getInt("kills"); // Return the players ammount of kills. If you wanted to get total (just a random number for an example for you guys) You would change this to total!
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// Exact same method here, Except as mentioned above i am looking for total!
|
||||
public Integer getTotal(String string) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '"+string+"';");
|
||||
|
||||
rs = ps.executeQuery();
|
||||
while(rs.next()){
|
||||
if(rs.getString("player").equalsIgnoreCase(string.toLowerCase())){
|
||||
return rs.getInt("total");
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Now we need methods to save things to the database
|
||||
public void setTokens(Player player, Integer tokens, Integer total) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("REPLACE INTO " + table + " (player,kills,total) VALUES(?,?,?)"); // IMPORTANT. In SQLite class, We made 3 colums. player, Kills, Total.
|
||||
ps.setString(1, player.getName().toLowerCase()); // YOU MUST put these into this line!! And depending on how many
|
||||
// colums you put (say you made 5) All 5 need to be in the brackets
|
||||
// Seperated with comma's (,) AND there needs to be the same amount of
|
||||
// question marks in the VALUES brackets. Right now i only have 3 colums
|
||||
// So VALUES (?,?,?) If you had 5 colums VALUES(?,?,?,?,?)
|
||||
|
||||
ps.setInt(2, tokens); // This sets the value in the database. The colums go in order. Player is ID 1, kills is ID 2, Total would be 3 and so on. you can use
|
||||
// setInt, setString and so on. tokens and total are just variables sent in, You can manually send values in as well. p.setInt(2, 10) <-
|
||||
// This would set the players kills instantly to 10. Sorry about the variable names, It sets their kills to 10 i just have the variable called
|
||||
// Tokens from another plugin :/
|
||||
ps.setInt(3, total);
|
||||
ps.executeUpdate();
|
||||
return;
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
public void close(PreparedStatement ps,ResultSet rs){
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
} catch (SQLException ex) {
|
||||
Error.close(plugin, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package me.william278.huskhomes2.Data.SQLite.Errors;
|
||||
|
||||
import me.william278.huskhomes2.Main;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Error {
|
||||
|
||||
public static void execute(Main plugin, Exception ex){
|
||||
plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement: ", ex);
|
||||
}
|
||||
public static void close(Main plugin, Exception ex){
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package me.william278.huskhomes2.Data.SQLite.Errors;
|
||||
|
||||
public class Errors {
|
||||
public static String sqlConnectionExecute(){
|
||||
return "Couldn't execute MySQL statement: ";
|
||||
}
|
||||
public static String sqlConnectionClose(){
|
||||
return "Failed to close MySQL connection: ";
|
||||
}
|
||||
public static String noSQLConnection(){
|
||||
return "Unable to retreive MYSQL connection: ";
|
||||
}
|
||||
public static String noTableFound(){
|
||||
return "Database Error: No Table Found";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package me.william278.huskhomes2.Data.SQLite;
|
||||
|
||||
import me.william278.huskhomes2.Main;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class SQLite extends Database {
|
||||
|
||||
final String databaseName = "HuskHomesData";
|
||||
|
||||
public SQLite(Main instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
public String createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerTable() + " (" +
|
||||
"`player_id` integer PRIMARY KEY," +
|
||||
"`uuid` text NOT NULL UNIQUE," +
|
||||
"`username` text NOT NULL," +
|
||||
"`home_count` integer NOT NULL," +
|
||||
"`home_slots` integer NOT NULL," +
|
||||
"`rtp_cooldown` integer NOT NULL," +
|
||||
"`home_count` integer NOT NULL" +
|
||||
");";
|
||||
|
||||
public String createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsTable() + " (" +
|
||||
"`location_id` integer PRIMARY KEY," +
|
||||
"`world` text NOT NULL," +
|
||||
"`server` text NOT NULL," +
|
||||
"`x` double NOT NULL," +
|
||||
"`y` double NOT NULL," +
|
||||
"`z` double NOT NULL," +
|
||||
"`yaw` float NOT NULL," +
|
||||
"`pitch` float NOT NULL" +
|
||||
");";
|
||||
|
||||
public String createHomesTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getHomesTable() + " (" +
|
||||
"`player_id` integer NOT NULL," +
|
||||
"`location_id` integer NOT NULL," +
|
||||
"`name` text NOT NULL," +
|
||||
"`description` double NOT NULL," +
|
||||
"`public` boolean NOT NULL," +
|
||||
|
||||
"PRIMARY KEY (`player_id`, `location_id`)," +
|
||||
"FOREIGN KEY (`player_id`) REFERENCES " + Main.settings.getPlayerTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
");";
|
||||
|
||||
public String createWarpsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getWarpsTable() + " (" +
|
||||
"`location_id` integer NOT NULL," +
|
||||
"`name` text NOT NULL," +
|
||||
"`description` double NOT NULL," +
|
||||
|
||||
"PRIMARY KEY (`location_id`)," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
");";
|
||||
|
||||
// SQL creation stuff, You can leave the blow stuff untouched.
|
||||
public Connection getSQLConnection() {
|
||||
File dataFolder = new File(plugin.getDataFolder(), databaseName + ".db");
|
||||
if (!dataFolder.exists()) {
|
||||
try {
|
||||
dataFolder.createNewFile();
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "File write error: " + databaseName + ".db");
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return connection;
|
||||
}
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
|
||||
return connection;
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library; please download and place this in the /lib folder.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
connection = getSQLConnection();
|
||||
try {
|
||||
Statement s = connection.createStatement();
|
||||
s.executeUpdate(createPlayerTable);
|
||||
s.executeUpdate(createLocationsTable);
|
||||
s.executeUpdate(createHomesTable);
|
||||
s.executeUpdate(createWarpsTable);
|
||||
s.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
initialize();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user