Guest User

Untitled

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