Database.java 8.4 KB

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