001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.util;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013: import java.util.Properties;
014:
015: //#ifdef JDK14
016: import javax.naming.Context;
017: import javax.naming.NamingException;
018: import javax.sql.DataSource;
019: import javax.sql.XAConnection; //#endif
020:
021: import org.h2.constant.ErrorCode;
022: import org.h2.message.Message;
023:
024: /**
025: * This is a utility class with JDBC helper functions.
026: */
027: public class JdbcUtils {
028:
029: public static void closeSilently(Statement stat) {
030: if (stat != null) {
031: try {
032: stat.close();
033: } catch (SQLException e) {
034: // ignore
035: }
036: }
037: }
038:
039: public static void closeSilently(Connection conn) {
040: if (conn != null) {
041: try {
042: conn.close();
043: } catch (SQLException e) {
044: // ignore
045: }
046: }
047: }
048:
049: public static void closeSilently(ResultSet rs) {
050: if (rs != null) {
051: try {
052: rs.close();
053: } catch (SQLException e) {
054: // ignore
055: }
056: }
057: }
058:
059: public static ResultSet getGeneratedKeys(Statement stat)
060: throws SQLException {
061: ResultSet rs = null;
062: //#ifdef JDK14
063: rs = stat.getGeneratedKeys();
064: //#endif
065: return rs;
066: }
067:
068: //#ifdef JDK14
069: public static void closeSilently(XAConnection conn) {
070: if (conn != null) {
071: try {
072: conn.close();
073: } catch (SQLException e) {
074: // ignore
075: }
076: }
077: }
078:
079: //#endif
080:
081: /**
082: * Create a new database connection with the given settings.
083: *
084: * @param driver the driver class name
085: * @param url the database URL
086: * @param user the user name
087: * @param password the password
088: * @return the database connection
089: */
090: public static Connection getConnection(String driver, String url,
091: String user, String password) throws SQLException {
092: Properties prop = new Properties();
093: prop.setProperty("user", user);
094: prop.setProperty("password", password);
095: return getConnection(driver, url, prop);
096: }
097:
098: public static Connection getConnection(String driver, String url,
099: Properties prop) throws SQLException {
100: if (!StringUtils.isNullOrEmpty(driver)) {
101: try {
102: Class d = ClassUtils.loadUserClass(driver);
103: if (java.sql.Driver.class.isAssignableFrom(d)) {
104: return DriverManager.getConnection(url, prop);
105: //#ifdef JDK14
106: } else if (javax.naming.Context.class
107: .isAssignableFrom(d)) {
108: // JNDI context
109: try {
110: Context context = (Context) d.newInstance();
111: DataSource ds = (DataSource) context
112: .lookup(url);
113: String user = prop.getProperty("user");
114: String password = prop.getProperty("password");
115: if (StringUtils.isNullOrEmpty(user)
116: && StringUtils.isNullOrEmpty(password)) {
117: return ds.getConnection();
118: } else {
119: return ds.getConnection(user, password);
120: }
121: } catch (InstantiationException e) {
122: throw Message.convert(e);
123: } catch (IllegalAccessException e) {
124: throw Message.convert(e);
125: } catch (NamingException e) {
126: throw Message.convert(e);
127: }
128: //#endif
129: } else {
130: // Don't know, but maybe it loaded a JDBC Driver
131: return DriverManager.getConnection(url, prop);
132: }
133: } catch (ClassNotFoundException e) {
134: throw Message.getSQLException(
135: ErrorCode.CLASS_NOT_FOUND_1,
136: new String[] { driver }, e);
137: } catch (NoClassDefFoundError e) {
138: throw Message.getSQLException(
139: ErrorCode.CLASS_NOT_FOUND_1,
140: new String[] { driver }, e);
141: }
142: }
143: return DriverManager.getConnection(url, prop);
144: }
145:
146: }
|