Database.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50;
  15. import java.sql.Connection;
  16. import java.sql.DriverManager;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.HashMap;
  20. import java.util.ArrayList;
  21. import java.sql.PreparedStatement;
  22. import com.gmail.nossr50.config.LoadProperties;
  23. public class Database {
  24. private mcMMO plugin;
  25. private String connectionString;
  26. public Database(mcMMO instance) {
  27. this.plugin = instance;
  28. this.connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass;
  29. // Load the driver instance
  30. try {
  31. Class.forName("com.mysql.jdbc.Driver");
  32. DriverManager.getConnection(connectionString);
  33. } catch (ClassNotFoundException e) {
  34. plugin.getServer().getLogger().warning(e.getLocalizedMessage());
  35. } catch (SQLException e) {
  36. plugin.getServer().getLogger().warning(e.getLocalizedMessage());
  37. System.out.println("SQLException: " + e.getMessage());
  38. System.out.println("SQLState: " + e.getSQLState());
  39. System.out.println("VendorError: " + e.getErrorCode());
  40. }
  41. }
  42. //Create the DB structure
  43. public void createStructure() {
  44. Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "huds` (`user_id` int(10) unsigned NOT NULL,"
  45. + "`hudtype` varchar(50) NOT NULL DEFAULT '',"
  46. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  47. Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
  48. + "`user` varchar(40) NOT NULL,"
  49. + "`lastlogin` int(32) unsigned NOT NULL,"
  50. + "`party` varchar(100) NOT NULL DEFAULT '',"
  51. + "PRIMARY KEY (`id`),"
  52. + "UNIQUE KEY `user` (`user`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
  53. Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "cooldowns` (`user_id` int(10) unsigned NOT NULL,"
  54. + "`taming` int(32) unsigned NOT NULL DEFAULT '0',"
  55. + "`mining` int(32) unsigned NOT NULL DEFAULT '0',"
  56. + "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0',"
  57. + "`repair` int(32) unsigned NOT NULL DEFAULT '0',"
  58. + "`unarmed` int(32) unsigned NOT NULL DEFAULT '0',"
  59. + "`herbalism` int(32) unsigned NOT NULL DEFAULT '0',"
  60. + "`excavation` int(32) unsigned NOT NULL DEFAULT '0',"
  61. + "`archery` int(32) unsigned NOT NULL DEFAULT '0',"
  62. + "`swords` int(32) unsigned NOT NULL DEFAULT '0',"
  63. + "`axes` int(32) unsigned NOT NULL DEFAULT '0',"
  64. + "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0',"
  65. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  66. Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "skills` (`user_id` int(10) unsigned NOT NULL,"
  67. + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
  68. + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
  69. + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
  70. + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
  71. + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
  72. + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
  73. + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
  74. + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
  75. + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
  76. + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
  77. + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
  78. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  79. Write("CREATE TABLE IF NOT EXISTS `" + LoadProperties.MySQLtablePrefix + "experience` (`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 + "spawn` (`user_id` int(10) NOT NULL,"
  93. + "`x` int(64) NOT NULL DEFAULT '0',"
  94. + "`y` int(64) NOT NULL DEFAULT '0',"
  95. + "`z` int(64) NOT NULL DEFAULT '0',"
  96. + "`world` varchar(50) NOT NULL DEFAULT '',"
  97. + "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  98. Write("DROP TABLE IF EXISTS `"+LoadProperties.MySQLtablePrefix+"skills2`");
  99. Write("DROP TABLE IF EXISTS `"+LoadProperties.MySQLtablePrefix+"experience2`");
  100. checkDatabaseStructure();
  101. }
  102. public void checkDatabaseStructure()
  103. {
  104. String sql = "SELECT * FROM `mcmmo_experience` ORDER BY `"+LoadProperties.MySQLtablePrefix+"experience`.`fishing` ASC LIMIT 0 , 30";
  105. ResultSet rs = null;
  106. HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
  107. try {
  108. Connection conn = DriverManager.getConnection(connectionString);
  109. PreparedStatement stmt = conn.prepareStatement(sql);
  110. if (stmt.executeQuery() != null) {
  111. stmt.executeQuery();
  112. rs = stmt.getResultSet();
  113. while (rs.next()) {
  114. ArrayList<String> Col = new ArrayList<String>();
  115. for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  116. Col.add(rs.getString(i));
  117. }
  118. Rows.put(rs.getRow(), Col);
  119. }
  120. }
  121. conn.close();
  122. } catch (SQLException ex) {
  123. System.out.println("Updating mcMMO MySQL tables...");
  124. Write("ALTER TABLE `"+LoadProperties.MySQLtablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  125. Write("ALTER TABLE `"+LoadProperties.MySQLtablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  126. }
  127. }
  128. // write query
  129. public boolean Write(String sql) {
  130. try {
  131. Connection conn = DriverManager.getConnection(connectionString);
  132. PreparedStatement stmt = conn.prepareStatement(sql);
  133. stmt.executeUpdate();
  134. conn.close();
  135. return true;
  136. } catch (SQLException ex) {
  137. System.out.println("SQLException: " + ex.getMessage());
  138. System.out.println("SQLState: " + ex.getSQLState());
  139. System.out.println("VendorError: " + ex.getErrorCode());
  140. return false;
  141. }
  142. }
  143. // Get Int
  144. // only return first row / first field
  145. public Integer GetInt(String sql) {
  146. ResultSet rs = null;
  147. Integer result = 0;
  148. try {
  149. Connection conn = DriverManager.getConnection(connectionString);
  150. PreparedStatement stmt = conn.prepareStatement(sql);
  151. stmt = conn.prepareStatement(sql);
  152. if (stmt.executeQuery() != null) {
  153. stmt.executeQuery();
  154. rs = stmt.getResultSet();
  155. if (rs.next()) {
  156. result = rs.getInt(1);
  157. } else {
  158. result = 0;
  159. }
  160. }
  161. conn.close();
  162. } catch (SQLException ex) {
  163. System.out.println("SQLException: " + ex.getMessage());
  164. System.out.println("SQLState: " + ex.getSQLState());
  165. System.out.println("VendorError: " + ex.getErrorCode());
  166. }
  167. return result;
  168. }
  169. // read query
  170. public HashMap<Integer, ArrayList<String>> Read(String sql) {
  171. ResultSet rs = null;
  172. HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
  173. try {
  174. Connection conn = DriverManager.getConnection(connectionString);
  175. PreparedStatement stmt = conn.prepareStatement(sql);
  176. if (stmt.executeQuery() != null) {
  177. stmt.executeQuery();
  178. rs = stmt.getResultSet();
  179. while (rs.next()) {
  180. ArrayList<String> Col = new ArrayList<String>();
  181. for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  182. Col.add(rs.getString(i));
  183. }
  184. Rows.put(rs.getRow(), Col);
  185. }
  186. }
  187. conn.close();
  188. } catch (SQLException ex) {
  189. System.out.println("SQLException: " + ex.getMessage());
  190. System.out.println("SQLState: " + ex.getSQLState());
  191. System.out.println("VendorError: " + ex.getErrorCode());
  192. }
  193. return Rows;
  194. }
  195. }