Advertisement
Guest User

Untitled

a guest
Dec 28th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.95 KB | None | 0 0
  1. package ru.slimetwitch.mysql;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.HashMap;
  11.  
  12. import org.sqlite.SQLiteConfig;
  13. import org.sqlite.SQLiteConfig.Encoding;
  14. import org.sqlite.SQLiteConfig.JournalMode;
  15.  
  16. import cn.nukkit.utils.Config;
  17. import cn.nukkit.utils.LogLevel;
  18. import ru.slimetwitch.core.FOCraft;
  19.  
  20. public class Connector {
  21.  
  22. private Connection connection;
  23. private String url;
  24. private String user;
  25. private String passwd;
  26.  
  27. private HashMap<String, PreparedStatement> preparedStatements;
  28. private boolean autoCommit;
  29.  
  30. /**
  31. * Creates new SQLite object, without opening connection
  32. * @param path Path to database file, ex: "plugins/YourPlugin/data.db"
  33. */
  34. public Connector(String path, String user, String pass) {
  35. this.url = path;
  36. this.user = user;
  37. this.passwd = pass;
  38. this.autoCommit = false;
  39. }
  40.  
  41. /**
  42. * Open connection
  43. * @return
  44. */
  45. public boolean openConnection() {
  46. preparedStatements = new HashMap<>();
  47. try {
  48. Class.forName("com.mysql.jdbc.Driver");
  49. SQLiteConfig config = new SQLiteConfig();
  50. config.setEncoding(Encoding.UTF8);
  51. config.setJournalMode(JournalMode.OFF);
  52. config.setCacheSize(4);
  53. connection = DriverManager.getConnection("jdbc:mysql://" + url, user, passwd);
  54. return true;
  55. } catch (SQLException ex) {
  56. log(LogLevel.WARNING, "Error connecting to database: '" + url + "'");
  57. ex.printStackTrace();
  58. return false;
  59. }
  60. catch (ClassNotFoundException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. return false;
  64. }
  65. }
  66. /**
  67. * Close connection
  68. * @return
  69. */
  70. public boolean closeConnection() {
  71. try {
  72. if (connection != null) connection.close();
  73. return true;
  74. } catch (Exception ex) {
  75. log(LogLevel.WARNING, "Error closing connection.");
  76. ex.printStackTrace();
  77. return false;
  78. }
  79. }
  80.  
  81. /**
  82. * Executes SQL update
  83. * @param sql SQL code
  84. * @return true if all is okay, false if error occured
  85. */
  86. public boolean executeUpdate(String sql) {
  87. Statement statement = null;
  88. try {
  89. statement = connection.createStatement();
  90. statement.executeUpdate(sql);
  91. return true;
  92. } catch (SQLException ex) {
  93. log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
  94. ex.printStackTrace();
  95. return false;
  96. } catch (NullPointerException ex) {
  97. log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
  98. log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
  99. + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
  100. return false;
  101. }
  102. }
  103. /**
  104. * Executes SQL query
  105. * @param sql SQL code
  106. * @return true if all is okay, false if error occured
  107. */
  108. public ResultSet executeQuery(String sql) {
  109. Statement statement = null;
  110. try {
  111. statement = connection.createStatement();
  112. ResultSet resultSet = statement.executeQuery(sql);
  113. if (resultSet == null || resultSet.isClosed() || !resultSet.isBeforeFirst()) return null;
  114. else return resultSet;
  115. } catch (SQLException ex) {
  116. log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
  117. ex.printStackTrace();
  118. return null;
  119. } catch (NullPointerException ex) {
  120. log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
  121. log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
  122. + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
  123. return null;
  124. }
  125. }
  126.  
  127. /**
  128. * Executes SQL update, you prepared earlier, without placeholders
  129. * @param name Name of prepared update
  130. * @return true if all is okay, false if error occured
  131. */
  132. public boolean executePreparedUpdate(String name) {
  133. if (!preparedStatements.containsKey(name)) {
  134. log(LogLevel.WARNING, "Update. No such prepared statement: " + name);
  135. return false;
  136. }
  137. PreparedStatement statement = null;
  138. try {
  139. statement = preparedStatements.get(name);
  140. statement.executeUpdate();
  141. return true;
  142. } catch (SQLException ex) {
  143. log(LogLevel.WARNING, "Error executing prepared update: " + name);
  144. ex.printStackTrace();
  145. return false;
  146. } catch (NullPointerException ex) {
  147. log(LogLevel.WARNING, "Error executing prepared update: " + name);
  148. log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
  149. + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
  150. return false;
  151. }
  152. }
  153. /**
  154. * Executes SQL query, you prepared earlier, without placeholders
  155. * @param name Name of prepared query
  156. * @return true if all is okay, false if error occured
  157. */
  158. public ResultSet executePreparedQuery(String name) {
  159. if (!preparedStatements.containsKey(name)) {
  160. log(LogLevel.WARNING, "Query. No such prepared statement: " + name);
  161. return null;
  162. }
  163. PreparedStatement statement = preparedStatements.get(name);
  164. try {
  165. ResultSet resultSet = statement.executeQuery();
  166. if (resultSet == null || resultSet.isClosed() || !resultSet.isBeforeFirst()) return null;
  167. else return resultSet;
  168. } catch (SQLException ex) {
  169. log(LogLevel.WARNING, "Error executing prepared query: " + name);
  170. ex.printStackTrace();
  171. return null;
  172. } catch (NullPointerException ex) {
  173. log(LogLevel.WARNING, "Error executing prepared query: " + name);
  174. log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
  175. + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
  176. return null;
  177. }
  178. }
  179. /**
  180. * Executes SQL update, you prepared earlier, including placeholders
  181. * @param name Name of prepared update
  182. * @param args Placeholders to replace '?'
  183. * @return true if all is okay, false if error occured
  184. */
  185. public boolean executePreparedUpdate(String name, String...args) {
  186. if (!preparedStatements.containsKey(name)) {
  187. log(LogLevel.WARNING, "Update. No such prepared statement: " + name);
  188. return false;
  189. }
  190. PreparedStatement statement = null;
  191. try {
  192. statement = preparedStatements.get(name);
  193. for (int i = 0; i < args.length; i++) statement.setString(i + 1, args[i]);
  194. statement.executeUpdate();
  195. return true;
  196. } catch (SQLException ex) {
  197. log(LogLevel.WARNING, "Error executing prepared update: " + name);
  198. ex.printStackTrace();
  199. return false;
  200. } catch (NullPointerException ex) {
  201. log(LogLevel.WARNING, "Error executing prepared update: " + name);
  202. log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
  203. + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
  204. return false;
  205. }
  206. }
  207. /**
  208. * Executes SQL query, you prepared earlier, including placeholders
  209. * @param name Name of prepared query
  210. * @param args Placeholders to replace '?'
  211. * @return true if all is okay, false if error occured
  212. */
  213. public ResultSet executePreparedQuery(String name, String...args) {
  214. if (!preparedStatements.containsKey(name)) {
  215. log(LogLevel.WARNING, "Query. No such prepared statement: " + name);
  216. return null;
  217. }
  218. PreparedStatement statement = preparedStatements.get(name);
  219. try {
  220. for (int i = 0; i < args.length; i++) statement.setString(i + 1, args[i]);
  221. ResultSet resultSet = statement.executeQuery();
  222. if (resultSet == null || resultSet.isClosed() || !resultSet.isBeforeFirst()) return null;
  223. else return resultSet;
  224. } catch (SQLException ex) {
  225. log(LogLevel.WARNING, "Error executing prepared query: " + name);
  226. ex.printStackTrace();
  227. return null;
  228. } catch (NullPointerException ex) {
  229. log(LogLevel.WARNING, "Error executing prepared query: " + name);
  230. log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
  231. + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
  232. return null;
  233. }
  234. }
  235.  
  236. /**
  237. * Prepare and store statement to execute it in future
  238. * @param name Name of prepared statement to identify
  239. * @param sql SQL code
  240. * @return true if all is okay, false if error occured
  241. */
  242. public boolean prepareStatement(String name, String sql) {
  243. try {
  244. PreparedStatement prStatement = connection.prepareStatement(sql);
  245. preparedStatements.put(name, prStatement);
  246. return true;
  247. } catch (SQLException ex) {
  248. log(LogLevel.WARNING, "Error preparing statement: '" + sql + "'");
  249. ex.printStackTrace();
  250. return false;
  251. }
  252. }
  253. /**
  254. * Remove prepared statement if exists
  255. * @param name Name of prepared statement
  256. */
  257. public void removePreparedStatement(String name) {
  258. preparedStatements.remove(name);
  259. }
  260.  
  261. /**
  262. * Enable auto-commit
  263. * @param set enable auto-commit?
  264. * @return true if all is okay, false if error occured
  265. */
  266. public boolean setAutoCommit(boolean set) {
  267. try {
  268. connection.setAutoCommit(set);
  269. autoCommit = set;
  270. return true;
  271. } catch (SQLException ex) {
  272. log(LogLevel.WARNING, "Error setting autocommit: " + set);
  273. ex.printStackTrace();
  274. return false;
  275. }
  276. }
  277. /**
  278. * Get auto-commit status
  279. * @return auto-commit status
  280. */
  281. public boolean getAutoCommit() {
  282. return autoCommit;
  283. }
  284. /**
  285. * Commit changes
  286. * @return true if all is okay, false if error occured
  287. */
  288. public boolean commit() {
  289. try {
  290. connection.commit();
  291. return true;
  292. } catch (SQLException ex) {
  293. log(LogLevel.WARNING, "Error committing.");
  294. ex.printStackTrace();
  295. return false;
  296. }
  297. }
  298. /**
  299. * Rollback changes
  300. * @return true if all is okay, false if error occured
  301. */
  302. public boolean rollback() {
  303. try {
  304. connection.rollback();
  305. return true;
  306. } catch (SQLException ex) {
  307. log(LogLevel.WARNING, "Error rollbacking.");
  308. ex.printStackTrace();
  309. return false;
  310. }
  311. }
  312.  
  313. public void log(LogLevel level, String log) {
  314. FOCraft.plugin.getServer().getLogger().log(level, "[SQLite] " + log);
  315. }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement