You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JdbcUtils.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.util;
  18. import javax.naming.Context;
  19. import javax.sql.DataSource;
  20. import javax.sql.XAConnection;
  21. import java.sql.Connection;
  22. import java.sql.DriverManager;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26. import java.util.Properties;
  27. /**
  28. * This is a utility class with JDBC helper functions.
  29. */
  30. public class JdbcUtils {
  31. private static final String[] DRIVERS = {"h2:", "org.h2.Driver", "Cache:",
  32. "com.intersys.jdbc.CacheDriver", "daffodilDB://", "in.co.daffodil.db.rmi.RmiDaffodilDBDriver",
  33. "daffodil", "in.co.daffodil.db.jdbc.DaffodilDBDriver", "db2:", "com.ibm.db2.jcc.DB2Driver",
  34. "derby:net:", "org.apache.derby.jdbc.ClientDriver", "derby://",
  35. "org.apache.derby.jdbc.ClientDriver", "derby:", "org.apache.derby.jdbc.EmbeddedDriver",
  36. "FrontBase:", "com.frontbase.jdbc.FBJDriver", "firebirdsql:", "org.firebirdsql.jdbc.FBDriver",
  37. "hsqldb:", "org.hsqldb.jdbcDriver", "informix-sqli:", "com.informix.jdbc.IfxDriver", "jtds:",
  38. "net.sourceforge.jtds.jdbc.Driver", "microsoft:", "com.microsoft.jdbc.sqlserver.SQLServerDriver",
  39. "mimer:", "com.mimer.jdbc.Driver", "mysql:", "com.mysql.jdbc.Driver", "odbc:",
  40. "sun.jdbc.odbc.JdbcOdbcDriver", "oracle:", "oracle.jdbc.driver.OracleDriver", "pervasive:",
  41. "com.pervasive.jdbc.v2.Driver", "pointbase:micro:", "com.pointbase.me.jdbc.jdbcDriver",
  42. "pointbase:", "com.pointbase.jdbc.jdbcUniversalDriver", "postgresql:", "org.postgresql.Driver",
  43. "sybase:", "com.sybase.jdbc3.jdbc.SybDriver", "sqlserver:",
  44. "com.microsoft.sqlserver.jdbc.SQLServerDriver", "teradata:", "com.ncr.teradata.TeraDriver",};
  45. private JdbcUtils() {
  46. // utility class
  47. }
  48. /**
  49. * Close a statement without throwing an exception.
  50. *
  51. * @param stat the statement or null
  52. */
  53. public static void closeSilently(Statement stat) {
  54. if (stat != null) {
  55. try {
  56. stat.close();
  57. } catch (SQLException e) {
  58. // ignore
  59. }
  60. }
  61. }
  62. /**
  63. * Close a connection without throwing an exception.
  64. *
  65. * @param conn the connection or null
  66. */
  67. public static void closeSilently(Connection conn) {
  68. if (conn != null) {
  69. try {
  70. conn.close();
  71. } catch (SQLException e) {
  72. // ignore
  73. }
  74. }
  75. }
  76. /**
  77. * Close a result set without throwing an exception.
  78. *
  79. * @param rs the result set or null
  80. */
  81. public static void closeSilently(ResultSet rs) {
  82. closeSilently(rs, false);
  83. }
  84. /**
  85. * Close a result set, and optionally its statement without throwing an
  86. * exception.
  87. *
  88. * @param rs the result set or null
  89. */
  90. public static void closeSilently(ResultSet rs, boolean closeStatement) {
  91. if (rs != null) {
  92. Statement stat = null;
  93. if (closeStatement) {
  94. try {
  95. stat = rs.getStatement();
  96. } catch (SQLException e) {
  97. // ignore
  98. }
  99. }
  100. try {
  101. rs.close();
  102. } catch (SQLException e) {
  103. // ignore
  104. }
  105. closeSilently(stat);
  106. }
  107. }
  108. /**
  109. * Close an XA connection set without throwing an exception.
  110. *
  111. * @param conn the XA connection or null
  112. */
  113. public static void closeSilently(XAConnection conn) {
  114. if (conn != null) {
  115. try {
  116. conn.close();
  117. } catch (SQLException e) {
  118. // ignore
  119. }
  120. }
  121. }
  122. /**
  123. * Open a new database connection with the given settings.
  124. *
  125. * @param driver the driver class name
  126. * @param url the database URL
  127. * @param user the user name
  128. * @param password the password
  129. * @return the database connection
  130. */
  131. public static Connection getConnection(String driver, String url, String user, String password)
  132. throws SQLException {
  133. Properties prop = new Properties();
  134. if (user != null) {
  135. prop.setProperty("user", user);
  136. }
  137. if (password != null) {
  138. prop.setProperty("password", password);
  139. }
  140. return getConnection(driver, url, prop);
  141. }
  142. /**
  143. * Escape table or schema patterns used for DatabaseMetaData functions.
  144. *
  145. * @param pattern the pattern
  146. * @return the escaped pattern
  147. */
  148. public static String escapeMetaDataPattern(String pattern) {
  149. if (pattern == null || pattern.length() == 0) {
  150. return pattern;
  151. }
  152. return StringUtils.replaceAll(pattern, "\\", "\\\\");
  153. }
  154. /**
  155. * Open a new database connection with the given settings.
  156. *
  157. * @param driver the driver class name
  158. * @param url the database URL
  159. * @param prop the properties containing at least the user name and password
  160. * @return the database connection
  161. */
  162. public static Connection getConnection(String driver, String url, Properties prop) throws SQLException {
  163. if (StringUtils.isNullOrEmpty(driver)) {
  164. JdbcUtils.load(url);
  165. } else {
  166. Class<?> d = Utils.loadClass(driver);
  167. if (java.sql.Driver.class.isAssignableFrom(d)) {
  168. return DriverManager.getConnection(url, prop);
  169. } else if (javax.naming.Context.class.isAssignableFrom(d)) {
  170. // JNDI context
  171. try {
  172. Context context = (Context) d.newInstance();
  173. DataSource ds = (DataSource) context.lookup(url);
  174. String user = prop.getProperty("user");
  175. String password = prop.getProperty("password");
  176. if (StringUtils.isNullOrEmpty(user) && StringUtils.isNullOrEmpty(password)) {
  177. return ds.getConnection();
  178. }
  179. return ds.getConnection(user, password);
  180. } catch (SQLException e) {
  181. throw e;
  182. } catch (Exception e) {
  183. throw new SQLException("Failed to get connection for " + url, e);
  184. }
  185. } else {
  186. // Don't know, but maybe it loaded a JDBC Driver
  187. return DriverManager.getConnection(url, prop);
  188. }
  189. }
  190. return DriverManager.getConnection(url, prop);
  191. }
  192. /**
  193. * Get the driver class name for the given URL, or null if the URL is
  194. * unknown.
  195. *
  196. * @param url the database URL
  197. * @return the driver class name
  198. */
  199. public static String getDriver(String url) {
  200. if (url.startsWith("jdbc:")) {
  201. url = url.substring("jdbc:".length());
  202. for (int i = 0; i < DRIVERS.length; i += 2) {
  203. String prefix = DRIVERS[i];
  204. if (url.startsWith(prefix)) {
  205. return DRIVERS[i + 1];
  206. }
  207. }
  208. }
  209. return null;
  210. }
  211. /**
  212. * Load the driver class for the given URL, if the database URL is known.
  213. *
  214. * @param url the database URL
  215. */
  216. public static void load(String url) {
  217. String driver = getDriver(url);
  218. if (driver != null) {
  219. Utils.loadClass(driver);
  220. }
  221. }
  222. }