Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. package net.hyperplay.SQLLib;
  2.  
  3. import java.sql.*;
  4.  
  5. public class SQLLib {
  6.  
  7. Connection con = null;
  8. PreparedStatement pst = null;
  9. Statement st = null;
  10. ResultSet rs = null;
  11.  
  12. String url = null;
  13. String user = null;
  14. String password = null;
  15.  
  16. public SQL(String url, String user, String password){
  17. this.url = url;
  18. this.user = user;
  19. this.password = password;
  20. }
  21.  
  22. public boolean createTable(String table) {
  23. try {
  24.  
  25. con = DriverManager.getConnection(url, user, password);
  26.  
  27. st = con.createStatement();
  28.  
  29. String query = "CREATE TABLE IF NOT EXISTS `" + table + "` ( cKey VARCHAR(255) NOT NULL, cValue VARCHAR(255),
  30. UNIQUE KEY(cKey) );";
  31. st.executeUpdate(query);
  32.  
  33. } catch (SQLException ex) {
  34.  
  35. return false;
  36.  
  37. } finally {
  38. try {
  39. if (rs != null) {
  40. rs.close();
  41. }
  42. if (pst != null) {
  43. pst.close();
  44. }
  45. if (con != null) {
  46. con.close();
  47. }
  48.  
  49. } catch (SQLException ex) {
  50.  
  51. return false;
  52.  
  53. }
  54. }
  55.  
  56. return true;
  57.  
  58. }
  59.  
  60. public boolean set(String table, String key, String value) {
  61. try {
  62.  
  63. if(!this.createTable(table)){
  64. return false;
  65. }
  66.  
  67. con = DriverManager.getConnection(url, user, password);
  68.  
  69. String query = "INSERT INTO `" + table + "` (cKey, cValue) VALUES (?, ?);";
  70. pst = con.prepareStatement(query);
  71.  
  72. pst.setString(1, key);
  73. pst.setString(2, value);
  74. pst.executeUpdate();
  75.  
  76. rs = pst.executeQuery();
  77.  
  78. } catch (SQLException ex) {
  79.  
  80. return false;
  81.  
  82. } finally {
  83. try {
  84. if (rs != null) {
  85. rs.close();
  86. }
  87. if (pst != null) {
  88. pst.close();
  89. }
  90. if (con != null) {
  91. con.close();
  92. }
  93.  
  94. } catch (SQLException ex) {
  95.  
  96. return false;
  97.  
  98. }
  99. }
  100.  
  101. return true;
  102.  
  103. }
  104.  
  105. public boolean set(String table, String key, int value) {
  106.  
  107. return this.set(table, key, String.valueOf(value));
  108.  
  109. }
  110.  
  111. public boolean set(String table, String key, boolean value) {
  112.  
  113. return this.set(table, key, String.valueOf(value));
  114.  
  115. }
  116.  
  117. public boolean set(String table, String key, double value) {
  118.  
  119. return this.set(table, key, String.valueOf(value));
  120.  
  121. }
  122.  
  123. public Object get(String table, String key){
  124. try {
  125. con = DriverManager.getConnection(url, user, password);
  126.  
  127. String query = "SELECT * FROM `" + table + "` WHERE cKey = ?;";
  128. pst = con.prepareStatement(query);
  129.  
  130. pst.setString(1, key);
  131. pst.executeUpdate();
  132.  
  133. rs = pst.executeQuery();
  134.  
  135. if(rs.getObject(1) == null){
  136. return null;
  137. }else{
  138. return rs.getObject(1);
  139. }
  140.  
  141. } catch (SQLException ex) {
  142.  
  143. return null;
  144.  
  145. } finally {
  146. try {
  147. if (rs != null) {
  148. rs.close();
  149. }
  150. if (pst != null) {
  151. pst.close();
  152. }
  153. if (con != null) {
  154. con.close();
  155. }
  156.  
  157. } catch (SQLException ex) {
  158.  
  159. return null;
  160.  
  161. }
  162. }
  163. }
  164.  
  165. public boolean removeTable(String table){
  166. try {
  167. con = DriverManager.getConnection(url, user, password);
  168.  
  169. String query = "DROP TABLE `" + table + "`;";
  170. pst = con.prepareStatement(query);
  171.  
  172. rs = pst.executeQuery();
  173.  
  174. rs.next();
  175.  
  176. return true;
  177.  
  178. } catch (SQLException ex) {
  179.  
  180. return false;
  181.  
  182. } finally {
  183. try {
  184. if (rs != null) {
  185. rs.close();
  186. }
  187. if (pst != null) {
  188. pst.close();
  189. }
  190. if (con != null) {
  191. con.close();
  192. }
  193.  
  194. } catch (SQLException ex) {
  195.  
  196. return false;
  197.  
  198. }
  199. }
  200. }
  201.  
  202. public boolean removeEntry(String table, String entry){
  203. try {
  204. con = DriverManager.getConnection(url, user, password);
  205.  
  206. String query = "DELETE FROM `" + table + "` WHERE cKey = ?;";
  207. pst = con.prepareStatement(query);
  208.  
  209. pst.setString(1, entry);
  210.  
  211. rs = pst.executeQuery();
  212.  
  213. rs.next();
  214.  
  215. return true;
  216.  
  217. } catch (SQLException ex) {
  218.  
  219. return false;
  220.  
  221. } finally {
  222. try {
  223. if (rs != null) {
  224. rs.close();
  225. }
  226. if (pst != null) {
  227. pst.close();
  228. }
  229. if (con != null) {
  230. con.close();
  231. }
  232.  
  233. } catch (SQLException ex) {
  234.  
  235. return false;
  236.  
  237. }
  238. }
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement