File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\JdbcUtils.java.html

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