mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 06:28:49 +00:00
Updated SQL handling
This commit is contained in:
1 parent
559e38fc4b
commit
9d902a8453
4 files changed
+138
-94
No files matched your search
@@ -14,9 +14,7 @@ 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;
|
||||
}
|
||||
@@ -28,31 +26,24 @@ public abstract class Database {
|
||||
public void initialize(){
|
||||
connection = getSQLConnection();
|
||||
try{
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE player = ?");
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE uuid = ?");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
close(ps,rs);
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to retreive connection", ex);
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to retrieve 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) {
|
||||
public ResultSet queryDatabase(String query) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
ResultSet rs;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '"+string+"';");
|
||||
|
||||
ps = conn.prepareStatement(query);
|
||||
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!
|
||||
}
|
||||
}
|
||||
return rs;
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
@@ -65,37 +56,11 @@ public abstract class Database {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return null;
|
||||
}
|
||||
// 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) {
|
||||
@@ -133,7 +98,7 @@ public abstract class Database {
|
||||
}
|
||||
|
||||
|
||||
public void close(PreparedStatement ps,ResultSet rs){
|
||||
public void close(PreparedStatement ps, ResultSet rs){
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.william278.huskhomes2.Data.SQLite;
|
||||
|
||||
import me.william278.huskhomes2.Main;
|
||||
import me.william278.huskhomes2.dataManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -18,48 +19,6 @@ public class SQLite extends Database {
|
||||
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");
|
||||
@@ -89,10 +48,10 @@ public class SQLite extends Database {
|
||||
connection = getSQLConnection();
|
||||
try {
|
||||
Statement s = connection.createStatement();
|
||||
s.executeUpdate(createPlayerTable);
|
||||
s.executeUpdate(createLocationsTable);
|
||||
s.executeUpdate(createHomesTable);
|
||||
s.executeUpdate(createWarpsTable);
|
||||
s.executeUpdate(dataManager.createPlayerTable);
|
||||
s.executeUpdate(dataManager.createLocationsTable);
|
||||
s.executeUpdate(dataManager.createHomesTable);
|
||||
s.executeUpdate(dataManager.createWarpsTable);
|
||||
s.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in new issue
Block a user