Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.UUID;
- public class Database {
- private static Database db;
- private Connection con;
- private RunnableQueueThread runnableQueue;
- public Database() {
- db = this;
- }
- public static Database getDb() { // Nützlich um die Datenbank Instanz einfach per Database.getDb() zu holen
- return db;
- }
- public boolean init() {
- ( runnableQueue = new RunnableQueueThread() ).start();
- if ( connect() ) {
- cacheBans();
- return true;
- }
- return false;
- }
- private boolean connect() {
- try {
- con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/minecraft", "root", "roflpwrofl123!" );
- Main.info( "Erfolgreich zur Datenbank verbunden" );
- return true;
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException beim Verbinden zur Datenbank!", e );
- }
- return false;
- }
- // Ab hier können Methoden für Datenbank Aktionen in folgender Weise hinzugefügt werden
- // public void setBan( final String admin, final UUID targetUUID, final String spielername, final String grund, final int dauer ) {
- // runnableQueue.offerRunnable( new Runnable() {
- //
- // @Override
- // public void run() {
- // hier Statement/PreparedStatement erstellen (mit con.prepare()) und Zeug machen
- // Sinnvoll für PreparedStatements sind "util" Methoden in dieser Klasse, damit man nicht jedes Mal alle möglichen Exception catchen muss
- // (dann besteht ja die halbe Klasse aus try-catch Konstrukten :/
- // Beispiele: prepare(String sql); setString(...); setInt(...) - Die Methoden rufen dann z.B. setInt() auf dem Statement auf
- // aber auch Methoden wie update() oder query(), letzteres kann ein ResultSet zurückgeben
- // & getString(), getInt(), closeStatement()
- // WICHTIG: Interaktionen mit Minecraft/Bukkit müssen wieder sync sein!
- // Dafür: Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() { public void run() { CODE } });
- // Holt sich Datum aus Dauer des Banns
- // Calendar cal = Calendar.getInstance(); // creates calendar
- // cal.setTime( new java.util.Date() ); // sets calendar time/date
- // cal.add( Calendar.HOUR_OF_DAY, dauer ); // adds x hours
- // long unixTime = cal.getTimeInMillis() / 1000L; // Warum durch 1000 dividieren?
- // debug( targetUUID );
- // debug( "uid string: " + targetUUID.toString() );
- // debug( spielername );
- // debug( grund );
- // debug( admin );
- //
- // PreparedStatement st = prepare( "INSERT INTO ban (Spielername, UID, Grund, Admin, Dauer) VALUES (?, ?, ?, ?, ?)" );
- // setString( st, 1, spielername );
- // setString( st, 2, targetUUID.toString() );
- // setString( st, 3, grund );
- // setString( st, 4, admin );
- // setLong( st, 5, cal.getTimeInMillis() );
- //
- // executeUpdate( st );
- // }});}
- private void cacheBans() {
- runnableQueue.offerRunnable( new Runnable() {
- @Override
- public void run() {
- PreparedStatement st = prepare( "SELECT count(*) AS Anzahl, Spielername, UID, Grund, Dauer, Admin FROM ban" );
- ResultSet res = executeQuery( st );
- while ( next( res ) ) {
- EconomyEntry.initMap( getInt( res, "Anzahl" ) );
- EconomyEntry.addBan( new EconomyEntry( UUID.fromString( getString( res, "UID" ) ), getString( res, "Admin" ), getString( res, "Grund" ), getLong( res, "Dauer" ) ) );
- }
- }
- } );
- }
- // public boolean isBanned( final UUID u) {
- // runnableQueue.offerRunnable( new Runnable() {
- //
- // @Override
- // public void run() {
- // debug( u.toString() );
- //
- // PreparedStatement st = prepare( "SELECT UID FROM ban WHERE UID = ?)" );
- // setString( st, 1, u.toString() );
- //
- // //if vorhanden && notExpired
- // //return true;
- // }
- // } );
- // //return false;
- // }
- // Zur Erklärung des Prinzips hier:
- // public void test() {
- // debug( "test() - 1" ); // Als erstes ausgegeben
- // runnableQueue.offerRunnable( new Runnable() { // Dieses neue Runnable Objekt wird zur Warteschlange hinzugefügt
- //
- // @Override
- // public void run() {
- // debug( "run()" ); // Als letztes ausgegeben, im RunnableQueue-Thread (separater Thread) ausgeführt -> nicht im Bukkit main thread, asynchron!!
- // }
- // } );
- // debug( "test() - 2" ); // Höchstwahrscheinlich als zweites ausgegeben
- // }
- private PreparedStatement prepare( String sql ) {
- try {
- return con.prepareStatement( sql );
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while preparing statement " + sql + ": ", e );
- }
- return null;
- }
- @SuppressWarnings( "unused" )
- private void setString( PreparedStatement st, int index, String s ) {
- try {
- st.setString( index, s );
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while setting String: " + s + ": ", e );
- }
- }
- private void setLong( PreparedStatement st, int index, long value ) {
- try {
- st.setLong( index, value );
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while inserting long: " + value + ": ", e );
- }
- }
- private void setObject( PreparedStatement st, int index, Object o ) {
- try {
- st.setObject( index, o );
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while setting Object (" + o.getClass() + ") :", e );
- }
- }
- private void executeUpdate( PreparedStatement st ) {
- try {
- st.executeUpdate();
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while executing update: ", e );
- }
- }
- private ResultSet executeQuery( PreparedStatement st ) {
- try {
- return st.executeQuery();
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while executing query: ", e );
- }
- return null;
- }
- private boolean next( ResultSet res ) {
- try {
- return res.next();
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while ResultSet.next(): ", e );
- }
- return false;
- }
- private String getString( ResultSet r, String columnName ) {
- try {
- return r.getString( columnName );
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while fetching String from ResultSet: ", e );
- }
- return null;
- }
- private Integer getInt( ResultSet r, String columnName ) {
- try {
- return r.getInt( columnName );
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while fetching Integer from ResultSet: ", e );
- }
- return null;
- }
- private Long getLong( ResultSet r, String columnName ) {
- try {
- return r.getLong( columnName );
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while fetching Long from ResultSet: ", e );
- }
- return null;
- }
- private void closeStatement( PreparedStatement st ) {
- if ( st != null )
- try {
- st.close();
- } catch ( SQLException e ) {
- Main.logThrowable( "SQLException while closing PreparedStatement: ", e );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement