Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. package de.norminator.masterplugin.database;
  2.  
  3. import java.sql.*;
  4. import java.util.UUID;
  5.  
  6. import de.norminator.masterplugin.Main;
  7.  
  8. public class Database {
  9.  
  10. private static Database db;
  11. private Connection con;
  12. private RunnableQueueThread runnableQueue;
  13.  
  14.  
  15. public Database() {
  16. db = this;
  17. }
  18.  
  19. public static Database getDb() { // Nützlich um die Datenbank Instanz einfach per Database.getDb() zu holen
  20. return db;
  21. }
  22.  
  23. public boolean init() {
  24. ( runnableQueue = new RunnableQueueThread() ).start();
  25. if ( connect() ) {
  26. cacheBans();
  27. return true;
  28. }
  29. return false;
  30. }
  31.  
  32. private boolean connect() {
  33. try {
  34. con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/minecraft", "root", "roflpwrofl123!" );
  35. Main.info( "Erfolgreich zur Datenbank verbunden" );
  36. return true;
  37. } catch ( SQLException e ) {
  38. Main.logThrowable( "SQLException beim Verbinden zur Datenbank!", e );
  39. }
  40. return false;
  41. }
  42.  
  43. // Ab hier können Methoden für Datenbank Aktionen in folgender Weise hinzugefügt werden
  44. // public void setBan( final String admin, final UUID targetUUID, final String spielername, final String grund, final int dauer ) {
  45. // runnableQueue.offerRunnable( new Runnable() {
  46. //
  47. // @Override
  48. // public void run() {
  49. // hier Statement/PreparedStatement erstellen (mit con.prepare()) und Zeug machen
  50. // Sinnvoll für PreparedStatements sind "util" Methoden in dieser Klasse, damit man nicht jedes Mal alle möglichen Exception catchen muss
  51. // (dann besteht ja die halbe Klasse aus try-catch Konstrukten :/
  52. // Beispiele: prepare(String sql); setString(...); setInt(...) - Die Methoden rufen dann z.B. setInt() auf dem Statement auf
  53. // aber auch Methoden wie update() oder query(), letzteres kann ein ResultSet zurückgeben
  54. // & getString(), getInt(), closeStatement()
  55.  
  56. // WICHTIG: Interaktionen mit Minecraft/Bukkit müssen wieder sync sein!
  57. // Dafür: Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() { public void run() { CODE } });
  58.  
  59. // Holt sich Datum aus Dauer des Banns
  60. // Calendar cal = Calendar.getInstance(); // creates calendar
  61. // cal.setTime( new java.util.Date() ); // sets calendar time/date
  62. // cal.add( Calendar.HOUR_OF_DAY, dauer ); // adds x hours
  63. // long unixTime = cal.getTimeInMillis() / 1000L; // Warum durch 1000 dividieren?
  64.  
  65. // debug( targetUUID );
  66. // debug( "uid string: " + targetUUID.toString() );
  67. // debug( spielername );
  68. // debug( grund );
  69. // debug( admin );
  70. //
  71. // PreparedStatement st = prepare( "INSERT INTO ban (Spielername, UID, Grund, Admin, Dauer) VALUES (?, ?, ?, ?, ?)" );
  72. // setString( st, 1, spielername );
  73. // setString( st, 2, targetUUID.toString() );
  74. // setString( st, 3, grund );
  75. // setString( st, 4, admin );
  76. // setLong( st, 5, cal.getTimeInMillis() );
  77. //
  78. // executeUpdate( st );
  79. // }});}
  80.  
  81. private void cacheBans() {
  82. runnableQueue.offerRunnable( new Runnable() {
  83.  
  84. @Override
  85. public void run() {
  86. PreparedStatement st = prepare( "SELECT count(*) AS Anzahl, Spielername, UID, Grund, Dauer, Admin FROM ban" );
  87. ResultSet res = executeQuery( st );
  88. while ( next( res ) ) {
  89. EconomyEntry.initMap( getInt( res, "Anzahl" ) );
  90. EconomyEntry.addBan( new EconomyEntry( UUID.fromString( getString( res, "UID" ) ), getString( res, "Admin" ), getString( res, "Grund" ), getLong( res, "Dauer" ) ) );
  91. }
  92. }
  93. } );
  94. }
  95.  
  96. // public boolean isBanned( final UUID u) {
  97. // runnableQueue.offerRunnable( new Runnable() {
  98. //
  99. // @Override
  100. // public void run() {
  101. // debug( u.toString() );
  102. //
  103. // PreparedStatement st = prepare( "SELECT UID FROM ban WHERE UID = ?)" );
  104. // setString( st, 1, u.toString() );
  105. //
  106. // //if vorhanden && notExpired
  107. // //return true;
  108. // }
  109. // } );
  110. // //return false;
  111. // }
  112.  
  113. // Zur Erklärung des Prinzips hier:
  114. // public void test() {
  115. // debug( "test() - 1" ); // Als erstes ausgegeben
  116. // runnableQueue.offerRunnable( new Runnable() { // Dieses neue Runnable Objekt wird zur Warteschlange hinzugefügt
  117. //
  118. // @Override
  119. // public void run() {
  120. // debug( "run()" ); // Als letztes ausgegeben, im RunnableQueue-Thread (separater Thread) ausgeführt -> nicht im Bukkit main thread, asynchron!!
  121. // }
  122. // } );
  123. // debug( "test() - 2" ); // Höchstwahrscheinlich als zweites ausgegeben
  124. // }
  125.  
  126. private PreparedStatement prepare( String sql ) {
  127. try {
  128. return con.prepareStatement( sql );
  129. } catch ( SQLException e ) {
  130. Main.logThrowable( "SQLException while preparing statement " + sql + ": ", e );
  131. }
  132. return null;
  133. }
  134.  
  135. @SuppressWarnings( "unused" )
  136. private void setString( PreparedStatement st, int index, String s ) {
  137. try {
  138. st.setString( index, s );
  139. } catch ( SQLException e ) {
  140. Main.logThrowable( "SQLException while setting String: " + s + ": ", e );
  141. }
  142. }
  143.  
  144. private void setLong( PreparedStatement st, int index, long value ) {
  145. try {
  146. st.setLong( index, value );
  147. } catch ( SQLException e ) {
  148. Main.logThrowable( "SQLException while inserting long: " + value + ": ", e );
  149. }
  150. }
  151.  
  152. private void setObject( PreparedStatement st, int index, Object o ) {
  153. try {
  154. st.setObject( index, o );
  155. } catch ( SQLException e ) {
  156. Main.logThrowable( "SQLException while setting Object (" + o.getClass() + ") :", e );
  157. }
  158. }
  159.  
  160. private void executeUpdate( PreparedStatement st ) {
  161. try {
  162. st.executeUpdate();
  163. } catch ( SQLException e ) {
  164. Main.logThrowable( "SQLException while executing update: ", e );
  165. }
  166. }
  167.  
  168. private ResultSet executeQuery( PreparedStatement st ) {
  169. try {
  170. return st.executeQuery();
  171. } catch ( SQLException e ) {
  172. Main.logThrowable( "SQLException while executing query: ", e );
  173. }
  174. return null;
  175. }
  176.  
  177. private boolean next( ResultSet res ) {
  178. try {
  179. return res.next();
  180. } catch ( SQLException e ) {
  181. Main.logThrowable( "SQLException while ResultSet.next(): ", e );
  182. }
  183. return false;
  184. }
  185.  
  186. private String getString( ResultSet r, String columnName ) {
  187. try {
  188. return r.getString( columnName );
  189. } catch ( SQLException e ) {
  190. Main.logThrowable( "SQLException while fetching String from ResultSet: ", e );
  191. }
  192. return null;
  193. }
  194.  
  195. private Integer getInt( ResultSet r, String columnName ) {
  196. try {
  197. return r.getInt( columnName );
  198. } catch ( SQLException e ) {
  199. Main.logThrowable( "SQLException while fetching Integer from ResultSet: ", e );
  200. }
  201. return null;
  202. }
  203.  
  204. private Long getLong( ResultSet r, String columnName ) {
  205. try {
  206. return r.getLong( columnName );
  207. } catch ( SQLException e ) {
  208. Main.logThrowable( "SQLException while fetching Long from ResultSet: ", e );
  209. }
  210. return null;
  211. }
  212.  
  213. private void closeStatement( PreparedStatement st ) {
  214. if ( st != null )
  215. try {
  216. st.close();
  217. } catch ( SQLException e ) {
  218. Main.logThrowable( "SQLException while closing PreparedStatement: ", e );
  219. }
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement