Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Properties;
  3. import java.io.InputStream;
  4. import javax.naming.InitialContext;
  5. import javax.sql.DataSource;
  6. import org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory;
  7.  
  8. private static String JDBC_DRIVER = "jdbc.driver";
  9. private static String JDBC_URL = "jdbc.url";
  10. private static String JDBC_USER = "jdbc.user";
  11. private static String JDBC_PASSWORD = "jdbc.password";
  12. private static Properties props = new Properties();
  13. private Connection connection = null;
  14. private Statement stat = null;
  15. private ResultSet rs = null;
  16. private static volatile DataSource dsObj;
  17. static {
  18. try {
  19. // a way to retrieve the data in
  20. // connection.properties found
  21. // in WEB-INF/classes
  22. InputStream is = DbConnector.class.getResourceAsStream("/connection.properties");
  23. props.load(is);
  24. //PropertyConfigurator.configure("log4j.properties");
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. try {
  29. Class.forName(props.getProperty(JDBC_DRIVER)).newInstance();
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. private static void initialize() {
  36. try {
  37. dsObj = BasicDataSourceFactory.createDataSource(props);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42.  
  43. /**
  44. * Constructor
  45. */
  46. public DbConnector() {
  47. try {
  48. initialize();
  49. this.connection = getConnection();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. /**
  56. * Returns DB Connection
  57. * @return Connection
  58. * @throws SQLException
  59. */
  60. public static Connection getConnectionFromPool() throws SQLException {
  61. Connection connection = null;
  62. // checking for null singleton instance
  63. if (null == dsObj) { // synchronized over class to make thread safe
  64. synchronized (DbConnector.class) {
  65. // double checking for making singleton instance thread safe
  66. if (null == dsObj) {
  67. initialize();
  68. }
  69. }
  70. }
  71. // getting connection from data sourceconnection = dsObj.getConnection();
  72. return connection;
  73. }
  74.  
  75. /**
  76. * Get Connection
  77. * @return Connection object
  78. * @throws SQLException
  79. */
  80. private Connection getConnection() throws SQLException {
  81. return DriverManager.getConnection(props.getProperty(JDBC_URL), props.getProperty(JDBC_USER), props.getProperty(JDBC_PASSWORD));
  82. }
  83.  
  84. /**
  85. * Execute Query
  86. * Purpose: SELECT
  87. * @param sql SQL Statement
  88. * @return ResultSet
  89. */
  90. public ResultSet executeQuery(String sql) {
  91. try {
  92. if (connection == null) {
  93. return null;
  94. }
  95. stat = connection.createStatement();
  96. rs = stat.executeQuery(sql);
  97. return rs;
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. return null;
  101. }
  102. }
  103.  
  104. /**
  105. * Execute Update
  106. * Purpose: Insert, Update, Delete
  107. * @param sql SQL Statement
  108. * @return int No. of Rows Updated
  109. */
  110. public int executeUpdate(String sql) {
  111. try {
  112. if (connection == null) {
  113. return -1;
  114. }
  115. stat = connection.createStatement();
  116. return stat.executeUpdate(sql);
  117. } catch (Exception e) {
  118. //e.printStackTrace();
  119.  
  120. return -1;
  121. }
  122. }
  123.  
  124. /**
  125. * Execute
  126. * Purpose: Create, Drop
  127. * @param sql statement to update.
  128. * @return true is statement execute sucessfuly and false otherwise
  129. */
  130. public boolean execute(String sql) {
  131. try {
  132. if (connection == null) {
  133. return false;
  134. }
  135. stat = connection.createStatement();
  136. return stat.execute(sql);
  137. } catch (Exception e) {
  138. //e.printStackTrace();
  139.  
  140. return false;
  141. }
  142. }
  143.  
  144. /**
  145. * Close ResultSet
  146. */
  147. public void closeResultSet() {
  148. if (rs != null) {
  149. try {
  150. rs.close();
  151. } catch (Exception e) {
  152. //e.printStackTrace();
  153.  
  154. }
  155. }
  156. }
  157.  
  158. /**
  159. * Close Statement
  160. */
  161. public void closeStatement() {
  162. if (stat != null) {
  163. try {
  164. stat.close();
  165.  
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. //log.error(e);
  169. }
  170. }
  171. }
  172.  
  173. /**
  174. * Close Connection
  175. */
  176. public void closeConnection() {
  177. try {
  178. connection.close();
  179. } catch (Exception e) {
  180. e.printStackTrace();
  181. }
  182. }
  183.  
  184. /**
  185. * Close
  186. * Connection, Statement and Resultset *
  187. */
  188. public void close() {
  189. try {
  190. if (rs != null) {
  191. rs.close();
  192. }
  193. } catch (SQLException e) {
  194. e.printStackTrace();
  195. }
  196. try {
  197. if (stat != null) {
  198. stat.close();
  199. }
  200. } catch (SQLException e) {
  201. e.printStackTrace();
  202. }
  203. try {
  204. if (connection != null) {
  205. connection.close();
  206. }
  207. } catch (SQLException e) {
  208. e.printStackTrace();
  209. }
  210. }
  211.  
  212. <resource-ref>
  213. <description>Customer Database</description>
  214. <res-ref-name>jdbc/CustomerDB</res-ref-name>
  215. <res-type>javax.sql.DataSource</res-type>
  216. <res-auth>Container</res-auth>
  217. </resource-ref>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement