Database.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package com.gmail.nossr50;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.PreparedStatement;
  7. import java.util.HashMap;
  8. import java.util.ArrayList;
  9. import java.util.Properties;
  10. import com.gmail.nossr50.config.LoadProperties;
  11. import com.gmail.nossr50.datatypes.DatabaseUpdate;
  12. public class Database {
  13. private mcMMO plugin;
  14. private String connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass;
  15. private boolean isConnected;
  16. private Connection conn = null;
  17. public Database(mcMMO instance) {
  18. connect(); //Connect to MySQL
  19. this.plugin = instance;
  20. // Load the driver instance
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver");
  23. DriverManager.getConnection(connectionString);
  24. }
  25. catch (ClassNotFoundException e) {
  26. plugin.getServer().getLogger().warning(e.getLocalizedMessage());
  27. }
  28. catch (SQLException ex) {
  29. plugin.getServer().getLogger().warning(ex.getLocalizedMessage());
  30. printErrors(ex);
  31. }
  32. }
  33. /**
  34. * Attempt to connect to the mySQL database.
  35. */
  36. public void connect() {
  37. try {
  38. System.out.println("[mcMMO] Attempting connection to MySQL...");
  39. Properties conProperties = new Properties();
  40. conProperties.put("autoReconnect", "true");
  41. conProperties.put("maxReconnects", "3");
  42. conn = DriverManager.getConnection(connectionString, conProperties);
  43. isConnected = true;
  44. System.out.println("[mcMMO] Connection to MySQL established!");
  45. }
  46. catch (SQLException ex) {
  47. isConnected = false;
  48. ex.printStackTrace();
  49. printErrors(ex);
  50. }
  51. }
  52. /**
  53. * Attempt to create the database structure.
  54. */
  55. public void createStructure() {
  56. write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "huds` (`user_id` int(10) unsigned NOT NULL,"
  57. + "`hudtype` varchar(50) NOT NULL DEFAULT '',"
  58. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  59. write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
  60. + "`user` varchar(40) NOT NULL,"
  61. + "`lastlogin` int(32) unsigned NOT NULL,"
  62. + "`party` varchar(100) NOT NULL DEFAULT '',"
  63. + "PRIMARY KEY (`id`),"
  64. + "UNIQUE KEY `user` (`user`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
  65. write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "cooldowns` (`user_id` int(10) unsigned NOT NULL,"
  66. + "`taming` int(32) unsigned NOT NULL DEFAULT '0',"
  67. + "`mining` int(32) unsigned NOT NULL DEFAULT '0',"
  68. + "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0',"
  69. + "`repair` int(32) unsigned NOT NULL DEFAULT '0',"
  70. + "`unarmed` int(32) unsigned NOT NULL DEFAULT '0',"
  71. + "`herbalism` int(32) unsigned NOT NULL DEFAULT '0',"
  72. + "`excavation` int(32) unsigned NOT NULL DEFAULT '0',"
  73. + "`archery` int(32) unsigned NOT NULL DEFAULT '0',"
  74. + "`swords` int(32) unsigned NOT NULL DEFAULT '0',"
  75. + "`axes` int(32) unsigned NOT NULL DEFAULT '0',"
  76. + "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0',"
  77. + "`blast_mining` int(32) unsigned NOT NULL DEFAULT '0',"
  78. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  79. write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "skills` (`user_id` int(10) unsigned NOT NULL,"
  80. + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
  81. + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
  82. + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
  83. + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
  84. + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
  85. + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
  86. + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
  87. + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
  88. + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
  89. + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
  90. + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
  91. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  92. write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "experience` (`user_id` int(10) unsigned NOT NULL,"
  93. + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
  94. + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
  95. + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
  96. + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
  97. + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
  98. + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
  99. + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
  100. + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
  101. + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
  102. + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
  103. + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
  104. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  105. write("DROP TABLE IF EXISTS `"+LoadProperties.MySQLtablePrefix+"skills2`");
  106. write("DROP TABLE IF EXISTS `"+LoadProperties.MySQLtablePrefix+"experience2`");
  107. write("DROP TABLE IF EXISTS `"+LoadProperties.MySQLtablePrefix+"spawn`");
  108. checkDatabaseStructure(DatabaseUpdate.FISHING);
  109. checkDatabaseStructure(DatabaseUpdate.BLAST_MINING);
  110. }
  111. /**
  112. * Check database structure for missing values.
  113. *
  114. * @param update Type of data to check updates for
  115. */
  116. public void checkDatabaseStructure(DatabaseUpdate update) {
  117. String sql = null;
  118. ResultSet rs = null;
  119. HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
  120. switch (update) {
  121. case BLAST_MINING:
  122. sql = "SELECT * FROM `"+LoadProperties.MySQLtablePrefix+"cooldowns` ORDER BY `"+LoadProperties.MySQLtablePrefix+"cooldowns`.`blast_mining` ASC LIMIT 0 , 30";
  123. break;
  124. case FISHING:
  125. sql = "SELECT * FROM `"+LoadProperties.MySQLtablePrefix+"experience` ORDER BY `"+LoadProperties.MySQLtablePrefix+"experience`.`fishing` ASC LIMIT 0 , 30";
  126. break;
  127. default:
  128. break;
  129. }
  130. try {
  131. PreparedStatement stmt = conn.prepareStatement(sql);
  132. if (stmt.executeQuery() != null) {
  133. stmt.executeQuery();
  134. rs = stmt.getResultSet();
  135. while (rs.next()) {
  136. ArrayList<String> Col = new ArrayList<String>();
  137. for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  138. Col.add(rs.getString(i));
  139. }
  140. Rows.put(rs.getRow(), Col);
  141. }
  142. }
  143. }
  144. catch (SQLException ex) {
  145. if (update.equals(DatabaseUpdate.BLAST_MINING)) {
  146. System.out.println("Updating mcMMO MySQL tables for Blast Mining...");
  147. write("ALTER TABLE `"+LoadProperties.MySQLtablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;");
  148. }
  149. else if (update.equals(DatabaseUpdate.FISHING)) {
  150. System.out.println("Updating mcMMO MySQL tables for Fishing...");
  151. write("ALTER TABLE `"+LoadProperties.MySQLtablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  152. write("ALTER TABLE `"+LoadProperties.MySQLtablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  153. }
  154. }
  155. }
  156. /**
  157. * Attempt to write the SQL query.
  158. *
  159. * @param sql Query to write.
  160. * @return true if the query was successfully written, false otherwise.
  161. */
  162. public boolean write(String sql) {
  163. if (conn != null) {
  164. try {
  165. PreparedStatement stmt = conn.prepareStatement(sql);
  166. stmt.executeUpdate();
  167. return true;
  168. }
  169. catch (SQLException ex) {
  170. printErrors(ex);
  171. return false;
  172. }
  173. }
  174. else {
  175. isConnected = false;
  176. connect(); //Attempt to reconnect
  177. if (isConnected) {
  178. write(sql); //Try the same operation again now that we are connected
  179. }
  180. else {
  181. System.out.println("[mcMMO] Unable to connect to MySQL! Make sure the SQL server is online!");
  182. }
  183. }
  184. return false;
  185. }
  186. /**
  187. * Get the Integer. Only return first row / first field.
  188. *
  189. * @param sql SQL query to execute
  190. * @return the value in the first row / first field
  191. */
  192. public Integer getInt(String sql) {
  193. ResultSet rs = null;
  194. Integer result = 0;
  195. if (conn != null) {
  196. try {
  197. PreparedStatement stmt = conn.prepareStatement(sql);
  198. stmt = conn.prepareStatement(sql);
  199. if (stmt.executeQuery() != null) {
  200. stmt.executeQuery();
  201. rs = stmt.getResultSet();
  202. if (rs.next()) {
  203. result = rs.getInt(1);
  204. }
  205. else {
  206. result = 0;
  207. }
  208. }
  209. }
  210. catch (SQLException ex) {
  211. printErrors(ex);
  212. }
  213. }
  214. else {
  215. isConnected = false;
  216. connect(); //Attempt to reconnect
  217. if (isConnected) {
  218. getInt(sql); //Try the same operation again now that we are connected
  219. }
  220. else {
  221. System.out.println("[mcMMO] Unable to connect to MySQL! Make sure the SQL server is online!");
  222. }
  223. }
  224. return result;
  225. }
  226. /**
  227. * Read SQL query.
  228. *
  229. * @param sql SQL query to read
  230. * @return the rows in this SQL query
  231. */
  232. public HashMap<Integer, ArrayList<String>> read(String sql) {
  233. ResultSet rs = null;
  234. HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
  235. if (conn != null) {
  236. try {
  237. PreparedStatement stmt = conn.prepareStatement(sql);
  238. if (stmt.executeQuery() != null) {
  239. stmt.executeQuery();
  240. rs = stmt.getResultSet();
  241. while (rs.next()) {
  242. ArrayList<String> Col = new ArrayList<String>();
  243. for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  244. Col.add(rs.getString(i));
  245. }
  246. Rows.put(rs.getRow(), Col);
  247. }
  248. }
  249. }
  250. catch (SQLException ex) {
  251. printErrors(ex);
  252. }
  253. }
  254. else {
  255. isConnected = false;
  256. connect(); //Attempt to reconnect
  257. if (isConnected) {
  258. read(sql); //Attempt the same operation again now that we are connected
  259. }
  260. else {
  261. System.out.println("[mcMMO] Unable to connect to MySQL! Make sure the SQL server is online!");
  262. }
  263. }
  264. return Rows;
  265. }
  266. private static void printErrors(SQLException ex) {
  267. System.out.println("SQLException: " + ex.getMessage());
  268. System.out.println("SQLState: " + ex.getSQLState());
  269. System.out.println("VendorError: " + ex.getErrorCode());
  270. }
  271. }