Hows my MySQL?

Discussion in 'Plugin Development' started by number1_Master, Aug 22, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    number1_Master

    I started hooking MySQL into one of my plugins (a custom / private one for my server), and I was wondering if I can get some feedback on it. I would really like to know if I can do better in some areas, and if some areas in my code were pretty good. Keep in mind, this is the first time I ever implemented MySQL into java.

    MySQL.java (open)
    Code:java
    1. package me.number1_Master.DGChatManager.Utils;
    2.  
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8.  
    9. publicclass MySQL
    10. {
    11. /*
    12. *Variables
    13. */
    14. static String url = "jdbc:mysql://localhost:3306/DGChatManager";
    15. static String usrName = "root";
    16. static String pass = "203215";
    17.  
    18. publicstaticbooleanconnected = false;
    19.  
    20. static Connection conn = null;
    21.  
    22. /*
    23. *MySQL Utilities
    24. */
    25. publicstaticvoid connect()
    26. {
    27. try
    28. {
    29. try
    30. {
    31. Class.forName("com.mysql.jdbc.Driver");
    32. }
    33. catch(Exception err)
    34. { Log.e("MySQL: " + err.getMessage()); }
    35. conn = DriverManager.getConnection(url, usrName, pass);
    36. connected = true;
    37. }
    38. catch(SQLException err)
    39. { Log.e("MySQL: connect()"); }
    40. }
    41. publicstaticvoid close()
    42. {
    43. try
    44. {
    45. conn.close();
    46. connected = false;
    47. Log.i("MySQL: Closing ...");
    48. }
    49. catch(SQLException err)
    50. { Log.e("MySQL: close()"); }
    51. }
    52. static ResultSet execute(String query)
    53. {
    54. try
    55. {
    56. PreparedStatement stmt = conn.prepareStatement(query);
    57.  
    58. if(query.contains("SELECT"))
    59. {
    60. stmt.executeQuery();
    61. return stmt.getResultSet();
    62. }
    63. else if(query.contains("INSERT") || query.contains("UPDATE") || query.contains("DELETE")) stmt.executeUpdate();
    64. else stmt.execute();
    65. }
    66. catch(SQLException err)
    67. { Log.e("MySQL: execute()"); }
    68. returnnull;
    69. }
    70. static Object convertResult(ResultSet rs, boolean type)
    71. {
    72. try
    73. {
    74. if(rs.next() && rs.getObject(1) != null)
    75. {
    76. if(type) o = rs.getString(1);
    77. else o = rs.getInt(1);
    78. rs.close();
    79. return o;
    80. }
    81. else
    82. {
    83. if(type) return null;
    84. elsereturn 0;
    85. }
    86. }
    87. catch(SQLException err)
    88. { Log.e("MySQL: convertResult(): " + err.getMessage()); }
    89. if(type) returnnull;
    90. elsereturn 0;
    91. }
    92. publicstaticvoid purge()
    93. {
    94. int amount = (Integer) convertResult(execute("SELECT COUNT(*) FROM DGChatManager"), false);
    95. if(amount == 0) return;
    96.  
    97. int purged = 0;
    98.  
    99. String curDate = (String) convertResult(execute("SELECT CURDATE() FROM DGChatManager LIMIT 1"), true);
    100.  
    101. for(int i = 1; i<=amount; i++)
    102. {
    103. String date = (String) convertResult(execute("SELECT date FROM DGChatManager WHERE id=" + i), true);
    104. if(date == null)
    105. {
    106. amount++;
    107. continue;
    108. }
    109.  
    110. String playerName = (String) convertResult(execute("SELECT playername FROM DGChatManager WHERE id=" + i), true);
    111. if(playerName.equals("number1_Master") || playerName.equals("---") || playerName.equals("---") || playerName.equals("---") || playerName.equals("---"))
    112. {
    113. continue;
    114. }
    115.  
    116. String[] dSplit = date.split("-");
    117. String[] cdSplit = curDate.split("-");
    118. int year = 0; int curYear = 0; int month = 0; int curMonth = 0;
    119. try
    120. {
    121. year = Integer.parseInt(dSplit[0]);
    122. curYear = Integer.parseInt(cdSplit[0]);
    123. month = Integer.parseInt(dSplit[1]);
    124. curMonth = Integer.parseInt(cdSplit[1]);
    125. }
    126. { Log.e(err.getMessage()); }
    127.  
    128. if(year < curYear || month < curMonth)
    129. {
    130. execute("DELETE FROM DGChatManager WHERE id=" + i);
    131. purged++;
    132. continue;
    133. }
    134. }
    135. Log.i("MySQL: " + purged + " deleted from database!");
    136. }
    137. /*
    138. *Add to MySQL!
    139. */
    140. publicstaticvoid addTable()
    141. {
    142. execute("CREATE TABLE IF NOT EXISTS DGChatManager (id INT NOT NULL AUTO_INCREMENT, playername VARCHAR(20) NOT NULL, medals INT NOT NULL, date VARCHAR(10) NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB");
    143.  
    144. if(getPlayer("number1_Master") == null)
    145. {
    146. addPlayer("number1_Master");
    147. addMedal("number1_Master");
    148. }
    149. if(getPlayer("---") == null)
    150. {
    151. addPlayer("---");
    152. addMedal("---");
    153. }
    154. if(getPlayer("---") == null)
    155. {
    156. addPlayer("---");
    157. addMedal("---");
    158. }
    159. if(getPlayer("---") == null)
    160. {
    161. addPlayer("---");
    162. addMedal("---");
    163. }
    164. }
    165. public static void addPlayer(String playerName)
    166. {
    167. execute("INSERT INTO DGChatManager ( playername, medals, date ) VALUES ( '" + playerName + "', 0, CURDATE())");
    168. }
    169. public static void addMedal(String playerName)
    170. {
    171. execute("UPDATE DGChatManager SET medals=medals+1 WHERE playername='" + playerName + "'");
    172. }
    173. /*
    174. *Get from MySQL!
    175. */
    176. public static String getPlayer(String playerName)
    177. {
    178. return (String) convertResult(execute("SELECT playername FROM DGChatManager WHERE playername='" + playerName + "'"), true);
    179. }
    180. public static int getMedals(String playerName)
    181. {
    182. return (Integer) convertResult(execute("SELECT medals FROM DGChatManager WHERE playername='" + playerName + "'"), false);
    183. }
    184. }
     
  2. I wouldn't use a boolean to see whether you have a connection or not, since the connection might close, even if you don't explicitly ask it to close. However, there's a method in the connection class that gives you the connection status:
    Code:
    if(conn.isValid(3))
        //is connected
    else
        //not connected
     
  3. Offline

    number1_Master

    Ahhhh! Thank you. So you are saying to create a method getConnection which return a boolean. The boolean would return true or false if conn.isValid();

    I gotcha!
     
Thread Status:
Not open for further replies.

Share This Page