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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 java.sql.Connection;
  19. import java.sql.DriverManager;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import java.util.Properties;
  24. import javax.naming.Context;
  25. import javax.sql.DataSource;
  26. import javax.sql.XAConnection;
  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.jdbc.net.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
  52. * the statement or null
  53. */
  54. public static void closeSilently(Statement stat) {
  55. if (stat != null) {
  56. try {
  57. stat.close();
  58. } catch (SQLException e) {
  59. // ignore
  60. }
  61. }
  62. }
  63. /**
  64. * Close a connection without throwing an exception.
  65. *
  66. * @param conn
  67. * the connection or null
  68. */
  69. public static void closeSilently(Connection conn) {
  70. if (conn != null) {
  71. try {
  72. conn.close();
  73. } catch (SQLException e) {
  74. // ignore
  75. }
  76. }
  77. }
  78. /**
  79. * Close a result set without throwing an exception.
  80. *
  81. * @param rs
  82. * the result set or null
  83. */
  84. public static void closeSilently(ResultSet rs) {
  85. closeSilently(rs, false);
  86. }
  87. /**
  88. * Close a result set, and optionally its statement without throwing an
  89. * exception.
  90. *
  91. * @param rs
  92. * the result set or null
  93. */
  94. public static void closeSilently(ResultSet rs, boolean closeStatement) {
  95. if (rs != null) {
  96. Statement stat = null;
  97. if (closeStatement) {
  98. try {
  99. stat = rs.getStatement();
  100. } catch (SQLException e) {
  101. // ignore
  102. }
  103. }
  104. try {
  105. rs.close();
  106. } catch (SQLException e) {
  107. // ignore
  108. }
  109. closeSilently(stat);
  110. }
  111. }
  112. /**
  113. * Close an XA connection set without throwing an exception.
  114. *
  115. * @param conn
  116. * the XA connection or null
  117. */
  118. public static void closeSilently(XAConnection conn) {
  119. if (conn != null) {
  120. try {
  121. conn.close();
  122. } catch (SQLException e) {
  123. // ignore
  124. }
  125. }
  126. }
  127. /**
  128. * Open a new database connection with the given settings.
  129. *
  130. * @param driver
  131. * the driver class name
  132. * @param url
  133. * the database URL
  134. * @param user
  135. * the user name
  136. * @param password
  137. * the password
  138. * @return the database connection
  139. */
  140. public static Connection getConnection(String driver, String url, String user, String password)
  141. throws SQLException {
  142. Properties prop = new Properties();
  143. if (user != null) {
  144. prop.setProperty("user", user);
  145. }
  146. if (password != null) {
  147. prop.setProperty("password", password);
  148. }
  149. return getConnection(driver, url, prop);
  150. }
  151. /**
  152. * Escape table or schema patterns used for DatabaseMetaData functions.
  153. *
  154. * @param pattern
  155. * the pattern
  156. * @return the escaped pattern
  157. */
  158. public static String escapeMetaDataPattern(String pattern) {
  159. if (pattern == null || pattern.length() == 0) {
  160. return pattern;
  161. }
  162. return StringUtils.replaceAll(pattern, "\\", "\\\\");
  163. }
  164. /**
  165. * Open a new database connection with the given settings.
  166. *
  167. * @param driver
  168. * the driver class name
  169. * @param url
  170. * the database URL
  171. * @param prop
  172. * the properties containing at least the user name and password
  173. * @return the database connection
  174. */
  175. public static Connection getConnection(String driver, String url, Properties prop) throws SQLException {
  176. if (StringUtils.isNullOrEmpty(driver)) {
  177. JdbcUtils.load(url);
  178. } else {
  179. Class<?> d = Utils.loadClass(driver);
  180. if (java.sql.Driver.class.isAssignableFrom(d)) {
  181. return DriverManager.getConnection(url, prop);
  182. } else if (javax.naming.Context.class.isAssignableFrom(d)) {
  183. // JNDI context
  184. try {
  185. Context context = (Context) d.newInstance();
  186. DataSource ds = (DataSource) context.lookup(url);
  187. String user = prop.getProperty("user");
  188. String password = prop.getProperty("password");
  189. if (StringUtils.isNullOrEmpty(user) && StringUtils.isNullOrEmpty(password)) {
  190. return ds.getConnection();
  191. }
  192. return ds.getConnection(user, password);
  193. } catch (SQLException e) {
  194. throw e;
  195. } catch (Exception e) {
  196. throw new SQLException("Failed to get connection for " + url, e);
  197. }
  198. } else {
  199. // Don't know, but maybe it loaded a JDBC Driver
  200. return DriverManager.getConnection(url, prop);
  201. }
  202. }
  203. return DriverManager.getConnection(url, prop);
  204. }
  205. /**
  206. * Get the driver class name for the given URL, or null if the URL is
  207. * unknown.
  208. *
  209. * @param url
  210. * the database URL
  211. * @return the driver class name
  212. */
  213. public static String getDriver(String url) {
  214. if (url.startsWith("jdbc:")) {
  215. url = url.substring("jdbc:".length());
  216. for (int i = 0; i < DRIVERS.length; i += 2) {
  217. String prefix = DRIVERS[i];
  218. if (url.startsWith(prefix)) {
  219. return DRIVERS[i + 1];
  220. }
  221. }
  222. }
  223. return null;
  224. }
  225. /**
  226. * Load the driver class for the given URL, if the database URL is known.
  227. *
  228. * @param url
  229. * the database URL
  230. */
  231. public static void load(String url) {
  232. String driver = getDriver(url);
  233. if (driver != null) {
  234. Utils.loadClass(driver);
  235. }
  236. }
  237. }