Database.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.Statement;
  7. import java.util.HashMap;
  8. import java.util.ArrayList;
  9. import java.sql.PreparedStatement;
  10. import org.bukkit.entity.Player;
  11. import com.avaje.ebeaninternal.server.lib.sql.DataSourceException;
  12. import com.gmail.nossr50.config.LoadProperties;
  13. public class Database {
  14. private Connection conn;
  15. private mcMMO plugin;
  16. public Database(mcMMO instance)
  17. {
  18. plugin = instance;
  19. // Load the driver instance
  20. try {
  21. Class.forName("com.mysql.jdbc.Driver").newInstance();
  22. } catch (Exception ex) {
  23. throw new DataSourceException("Failed to initialize JDBC driver");
  24. }
  25. // make the connection
  26. try {
  27. conn = DriverManager.getConnection("jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass);
  28. } catch (SQLException ex) {
  29. // handle any errors
  30. System.out.println("SQLException: " + ex.getMessage());
  31. System.out.println("SQLState: " + ex.getSQLState());
  32. System.out.println("VendorError: " + ex.getErrorCode());
  33. }
  34. }
  35. //Create the DB structure
  36. public void createStructure(){
  37. Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT," +
  38. "`user` varchar(40) NOT NULL," +
  39. "`lastlogin` int(32) unsigned NOT NULL," +
  40. "`party` varchar(100) NOT NULL DEFAULT ''," +
  41. "PRIMARY KEY (`id`)," +
  42. "UNIQUE KEY `user` (`user`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
  43. Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"cooldowns` (`user_id` int(10) unsigned NOT NULL," +
  44. "`taming` int(32) unsigned NOT NULL DEFAULT '0'," +
  45. "`mining` int(32) unsigned NOT NULL DEFAULT '0'," +
  46. "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0'," +
  47. "`repair` int(32) unsigned NOT NULL DEFAULT '0'," +
  48. "`unarmed` int(32) unsigned NOT NULL DEFAULT '0'," +
  49. "`herbalism` int(32) unsigned NOT NULL DEFAULT '0'," +
  50. "`excavation` int(32) unsigned NOT NULL DEFAULT '0'," +
  51. "`archery` int(32) unsigned NOT NULL DEFAULT '0'," +
  52. "`swords` int(32) unsigned NOT NULL DEFAULT '0'," +
  53. "`axes` int(32) unsigned NOT NULL DEFAULT '0'," +
  54. "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0'," +
  55. "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  56. Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"skills` (`user_id` int(10) unsigned NOT NULL," +
  57. "`taming` int(10) unsigned NOT NULL DEFAULT '0'," +
  58. "`mining` int(10) unsigned NOT NULL DEFAULT '0'," +
  59. "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," +
  60. "`repair` int(10) unsigned NOT NULL DEFAULT '0'," +
  61. "`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," +
  62. "`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," +
  63. "`excavation` int(10) unsigned NOT NULL DEFAULT '0'," +
  64. "`archery` int(10) unsigned NOT NULL DEFAULT '0'," +
  65. "`swords` int(10) unsigned NOT NULL DEFAULT '0'," +
  66. "`axes` int(10) unsigned NOT NULL DEFAULT '0'," +
  67. "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," +
  68. "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  69. Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"experience` (`user_id` int(10) unsigned NOT NULL," +
  70. "`taming` int(10) unsigned NOT NULL DEFAULT '0'," +
  71. "`mining` int(10) unsigned NOT NULL DEFAULT '0'," +
  72. "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," +
  73. "`repair` int(10) unsigned NOT NULL DEFAULT '0'," +
  74. "`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," +
  75. "`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," +
  76. "`excavation` int(10) unsigned NOT NULL DEFAULT '0'," +
  77. "`archery` int(10) unsigned NOT NULL DEFAULT '0'," +
  78. "`swords` int(10) unsigned NOT NULL DEFAULT '0'," +
  79. "`axes` int(10) unsigned NOT NULL DEFAULT '0'," +
  80. "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," +
  81. "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  82. Write("CREATE TABLE IF NOT EXISTS `"+LoadProperties.MySQLtablePrefix+"spawn` (`user_id` int(10) NOT NULL," +
  83. "`x` int(64) NOT NULL DEFAULT '0'," +
  84. "`y` int(64) NOT NULL DEFAULT '0'," +
  85. "`z` int(64) NOT NULL DEFAULT '0'," +
  86. "`world` varchar(50) NOT NULL DEFAULT ''," +
  87. "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  88. }
  89. // check if its closed
  90. private void reconnect()
  91. {
  92. System.out.println("[mcMMO] Reconnecting to MySQL...");
  93. try
  94. {
  95. conn = DriverManager.getConnection("jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass);
  96. System.out.println("[mcMMO] Connection success!");
  97. } catch (SQLException ex)
  98. {
  99. System.out.println("[mcMMO] Connection to MySQL failed! Check status of MySQL server!");
  100. System.out.println("SQLException: " + ex.getMessage());
  101. System.out.println("SQLState: " + ex.getSQLState());
  102. System.out.println("VendorError: " + ex.getErrorCode());
  103. }
  104. try {
  105. if(conn.isValid(5)){
  106. Users.clearUsers();
  107. for(Player x : plugin.getServer().getOnlinePlayers())
  108. {
  109. Users.addUser(x);
  110. }
  111. }
  112. } catch (SQLException e) {
  113. //Herp
  114. }
  115. }
  116. // write query
  117. public boolean Write(String sql)
  118. {
  119. /*
  120. * Double check connection to MySQL
  121. */
  122. try
  123. {
  124. if(!conn.isValid(5))
  125. {
  126. reconnect();
  127. }
  128. } catch (SQLException e)
  129. {
  130. e.printStackTrace();
  131. }
  132. try
  133. {
  134. PreparedStatement stmt = null;
  135. stmt = this.conn.prepareStatement(sql);
  136. stmt.executeUpdate();
  137. return true;
  138. } catch(SQLException ex) {
  139. System.out.println("SQLException: " + ex.getMessage());
  140. System.out.println("SQLState: " + ex.getSQLState());
  141. System.out.println("VendorError: " + ex.getErrorCode());
  142. return false;
  143. }
  144. }
  145. // Get Int
  146. // only return first row / first field
  147. public Integer GetInt(String sql) {
  148. PreparedStatement stmt = null;
  149. ResultSet rs = null;
  150. Integer result = 0;
  151. /*
  152. * Double check connection to MySQL
  153. */
  154. try
  155. {
  156. if(!conn.isValid(5))
  157. {
  158. reconnect();
  159. }
  160. } catch (SQLException e)
  161. {
  162. e.printStackTrace();
  163. }
  164. try {
  165. stmt = this.conn.prepareStatement(sql);
  166. if (stmt.executeQuery() != null) {
  167. stmt.executeQuery();
  168. rs = stmt.getResultSet();
  169. if(rs.next()){
  170. result = rs.getInt(1);
  171. }
  172. else { result = 0; }
  173. }
  174. }
  175. catch (SQLException ex) {
  176. System.out.println("SQLException: " + ex.getMessage());
  177. System.out.println("SQLState: " + ex.getSQLState());
  178. System.out.println("VendorError: " + ex.getErrorCode());
  179. }
  180. return result;
  181. }
  182. // read query
  183. public HashMap<Integer, ArrayList<String>> Read(String sql) {
  184. /*
  185. * Double check connection to MySQL
  186. */
  187. try
  188. {
  189. if(!conn.isValid(5))
  190. {
  191. reconnect();
  192. }
  193. } catch (SQLException e)
  194. {
  195. e.printStackTrace();
  196. }
  197. PreparedStatement stmt = null;
  198. ResultSet rs = null;
  199. HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
  200. try {
  201. stmt = this.conn.prepareStatement(sql);
  202. if (stmt.executeQuery() != null) {
  203. stmt.executeQuery();
  204. rs = stmt.getResultSet();
  205. while (rs.next()) {
  206. ArrayList<String> Col = new ArrayList<String>();
  207. for(int i=1;i<=rs.getMetaData().getColumnCount();i++) {
  208. Col.add(rs.getString(i));
  209. }
  210. Rows.put(rs.getRow(),Col);
  211. }
  212. }
  213. }
  214. catch (SQLException ex) {
  215. System.out.println("SQLException: " + ex.getMessage());
  216. System.out.println("SQLState: " + ex.getSQLState());
  217. System.out.println("VendorError: " + ex.getErrorCode());
  218. }
  219. // release dataset
  220. if (rs != null) {
  221. try {
  222. rs.close();
  223. } catch (SQLException sqlEx) { } // ignore
  224. rs = null;
  225. }
  226. if (stmt != null) {
  227. try {
  228. stmt.close();
  229. } catch (SQLException sqlEx) { } // ignore
  230. stmt = null;
  231. }
  232. return Rows;
  233. }
  234. }