Advertisement
Guest User

Untitled

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