Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. private static DataSource datasource;
  4. private static String connection;
  5. public MySQL (){
  6. connection = "jdbc:mysql://" + Props.getDbHost() + ":" + Props.getDbPort() + "/" + Props.getDbName();
  7. //connection = "jdbc:mysql://localhost:3306/kwik_virtuals";
  8.  
  9. Logging.info("jdbc:mysql://" + Props.getDbHost() + ":" + Props.getDbPort() + "/" + Props.getDbName())
  10. init(connection,Configs.getDbUsername(),Configs.getDbPassword(),Configs.getMaxConnections());
  11. }
  12.  
  13. public synchronized static ArrayList<HashMap<String, String>> query(Connection connection, String query) {
  14.  
  15. Connection conn = null;
  16. Statement stmt = null;
  17. ResultSet rs = null;
  18. ArrayList<HashMap<String, String>> results = new ArrayList<>();
  19. //Logging.info("Running query :: " + query);
  20.  
  21. try {
  22. conn = connection;
  23. stmt = conn.createStatement();
  24. rs = stmt.executeQuery(query);
  25.  
  26. if (rs.next()) {
  27. //Logging.info("Result set not null.");
  28. ResultSetMetaData metaData = rs.getMetaData();
  29. String[] columns = new String[metaData.getColumnCount()];
  30.  
  31. for (int i = 1; i <= metaData.getColumnCount(); i++) {
  32. columns[i - 1] = metaData.getColumnLabel(i);
  33. }
  34.  
  35. rs.beforeFirst();
  36.  
  37. while (rs.next()) {
  38. HashMap<String, String> record = new HashMap<String, String>();
  39.  
  40. for (String col : columns) {
  41. record.put(col, rs.getString(col));
  42. }
  43.  
  44. results.add(record);
  45. }
  46. }
  47.  
  48. rs.close();
  49. stmt.close();
  50. conn.close();
  51.  
  52. } catch (SQLException e) {
  53. Logging.error(MySQL.class.getName() + " " + e.getMessage());
  54. return null;
  55. } catch (Exception ex) {
  56. Logging.error(MySQL.class.getName() + " " + ex.getMessage(), ex);
  57. return null;
  58. } finally {
  59.  
  60. if (rs != null) {
  61. try {
  62. rs.close();
  63. } catch (SQLException ex) {
  64. Logging.fatal(MySQL.class.getName() + " " + ex.getMessage());
  65. }
  66. }
  67.  
  68. if (stmt != null) {
  69. try {
  70. stmt.close();
  71. } catch (SQLException ex) {
  72. Logging.fatal(MySQL.class.getName() + " " + ex.getMessage());
  73. }
  74. }
  75.  
  76. if (conn != null) {
  77. try {
  78. conn.close();
  79. } catch (SQLException ex) {
  80. Logging.fatal(MySQL.class.getName() + " " + ex.getMessage());
  81. }
  82. }
  83. }
  84.  
  85. //Logging.info("Found query results returning :: " + results.size());
  86. return results;
  87. }
  88.  
  89. public synchronized static boolean recordExists(Connection connection, String query) {
  90.  
  91. Connection conn = null;
  92. Statement stmt = null;
  93. ResultSet rs = null;
  94. Boolean isPresent = false;
  95. //Logging.info("Running query ::" + query);
  96.  
  97. try {
  98. conn = connection;
  99. stmt = conn.createStatement();
  100. rs = stmt.executeQuery(query);
  101.  
  102. if (rs.next()) {
  103. //Logging.info("ResultSet not null");
  104. isPresent = true;
  105. }
  106.  
  107. rs.close();
  108. stmt.close();
  109. conn.close();
  110. } catch (SQLException ex) {
  111. Logging.error(MySQL.class.getName() + " " + ex.getMessage(), ex);
  112. return isPresent;
  113. } catch (Exception ex) {
  114. Logging.error(MySQL.class.getName() + " " + ex.getMessage(), ex);
  115. return isPresent;
  116. } finally {
  117.  
  118. if (rs != null) {
  119. try {
  120. rs.close();
  121. } catch (SQLException ex) {
  122. Logging.info(MySQL.class.getName() + " " + ex.getMessage());
  123. }
  124. }
  125.  
  126. if (stmt != null) {
  127. try {
  128. stmt.close();
  129. } catch (SQLException ex) {
  130. Logging.info(MySQL.class.getName() + " " + ex.getMessage());
  131. }
  132. }
  133.  
  134. if (conn != null) {
  135. try {
  136. conn.close();
  137. } catch (SQLException ex) {
  138. Logging.info(MySQL.class.getName() + " " + ex.getMessage());
  139. }
  140. }
  141. }
  142.  
  143. //Logging.info("Found query results returning :: " + isPresent);
  144. return isPresent;
  145. }
  146.  
  147. public synchronized static String update(Connection connection, String query) {
  148. Connection conn = null;
  149. Statement stmt = null;
  150. ResultSet rs = null;
  151. String autoIncKeyFromDb = null;
  152. //Logging.info("Update query running :: " + query);
  153.  
  154. try {
  155. conn = connection;
  156. stmt = conn.createStatement();
  157. stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
  158.  
  159. rs = stmt.getGeneratedKeys();
  160.  
  161. if (rs.next()) {
  162. autoIncKeyFromDb = rs.getString(1);
  163. }
  164.  
  165. //Logging.info("Auto increment key from db :: " + autoIncKeyFromDb + " from query :: " + query);
  166. rs.close();
  167. stmt.close();
  168. conn.close();
  169. } catch (SQLException ex) {
  170. Logging.error(MySQL.class.getName() + " " + query + " " + ex.getMessage(), ex);
  171. } catch (Exception e) {
  172. Logging.error(MySQL.class.getName() + " " + query + " " + e.getMessage(), e);
  173. } finally {
  174.  
  175. if (rs != null) {
  176. try {
  177. rs.close();
  178. } catch (SQLException e) {
  179. Logging.fatal(MySQL.class.getName() + " " + e.getMessage());
  180. }
  181. }
  182.  
  183. if (stmt != null) {
  184. try {
  185. stmt.close();
  186. } catch (SQLException e) {
  187. Logging.fatal(MySQL.class.getName() + " " + e.getMessage());
  188. }
  189. }
  190.  
  191. if (conn != null) {
  192. try {
  193. conn.close();
  194. } catch (SQLException e) {
  195. Logging.fatal(MySQL.class.getName() + " " + e.getMessage());
  196. }
  197. }
  198. }
  199.  
  200. return autoIncKeyFromDb;
  201. }
  202.  
  203. public static void init(String url,String username,String password,int maxConnections){
  204. PoolProperties p = new PoolProperties();
  205. p.setUrl(url);
  206. p.setDefaultAutoCommit(true);
  207. p.setDriverClassName("com.mysql.jdbc.Driver");
  208. p.setUsername(username);
  209. p.setPassword(password);
  210. p.setJmxEnabled(true);
  211. p.setTestWhileIdle(false);
  212. p.setTestOnBorrow(true);
  213. p.setValidationQuery("SELECT 1 FROM DUAL");
  214. p.setTestOnReturn(false);
  215. p.setValidationInterval(60000);
  216. p.setTimeBetweenEvictionRunsMillis(30000);
  217. p.setMaxActive(maxConnections);
  218. p.setInitialSize(10);
  219. p.setMaxWait(60000);
  220. p.setMinEvictableIdleTimeMillis(5000);
  221. p.setMinIdle(maxConnections);
  222. p.setMaxIdle(maxConnections);
  223. p.setLogAbandoned(false);
  224. p.setRemoveAbandoned(false);
  225.  
  226. p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
  227. "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;"+
  228. "org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer");
  229. try {
  230. datasource = new DataSource(p);
  231. datasource.setPoolProperties(p);
  232. datasource.createPool();
  233. } catch (SQLException ex) {
  234. System.err.println(MySQL.class.getName()+" "+ex);
  235. }
  236.  
  237. }
  238. public static Connection getConnection() throws SQLException {
  239. Connection conn = null;
  240. try {
  241. conn = datasource.getConnection();
  242. System.err.println("connection availability "+conn);
  243. Logging.info("connection availability "+conn);
  244. }catch(SQLException e) {
  245. Logging.error("connection availability failed "+e.getMessage(),e);
  246. System.err.println("connection availability failed "+e.getMessage());
  247. init(connection,Props.getDbUserName(),Props.getDbPassword(),Props.getMaxConnections());
  248. conn = datasource.getConnection();
  249. }
  250. return conn;
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement