List of usage examples for java.sql DatabaseMetaData getDriverName
String getDriverName() throws SQLException;
From source file:com.manydesigns.portofino.model.database.ConnectionProvider.java
public void init(DatabasePlatformsRegistry databasePlatformsRegistry) { Connection conn = null;// w ww . ja v a 2 s . c om ResultSet typeRs = null; String databaseName = getDatabase().getDatabaseName(); try { conn = acquireConnection(); DatabaseMetaData metadata = conn.getMetaData(); databaseProductName = metadata.getDatabaseProductName(); databaseProductVersion = metadata.getDatabaseProductVersion(); try { databaseMajorVersion = metadata.getDatabaseMajorVersion(); databaseMinorVersion = metadata.getDatabaseMinorVersion(); databaseMajorMinorVersion = MessageFormat.format("{0}.{1}", databaseMajorVersion, databaseMinorVersion); } catch (SQLException e) { databaseMajorMinorVersion = e.getMessage(); } driverName = metadata.getDriverName(); driverVersion = metadata.getDriverVersion(); driverMajorVersion = metadata.getDriverMajorVersion(); driverMinorVersion = metadata.getDriverMinorVersion(); driverMajorMinorVersion = MessageFormat.format("{0}.{1}", driverMajorVersion, driverMinorVersion); try { JDBCMajorVersion = metadata.getJDBCMajorVersion(); JDBCMinorVersion = metadata.getJDBCMinorVersion(); JDBCMajorMinorVersion = MessageFormat.format("{0}.{1}", JDBCMajorVersion, JDBCMinorVersion); } catch (Throwable e) { JDBCMajorMinorVersion = e.getMessage(); } // extract supported types types.clear(); typeRs = metadata.getTypeInfo(); while (typeRs.next()) { readType(typeRs); } fixMissingTypeAliases(types); Collections.sort(types, new TypeComparator()); databasePlatform = databasePlatformsRegistry.findApplicableAbstraction(this); if (databasePlatform == null) { status = STATUS_ERROR; errorMessage = MessageFormat.format("Database platform not found for {0}", databaseProductName); logger.warn(errorMessage); } else { status = STATUS_CONNECTED; errorMessage = null; } } catch (Throwable e) { status = STATUS_ERROR; errorMessage = e.getMessage(); logger.warn("Could not create database platform for " + databaseName, e); } finally { DbUtil.closeResultSetAndStatement(typeRs); releaseConnection(conn); lastTested = new Date(); } }
From source file:net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.java
/** * /*from ww w .j a va2 s.c o m*/ */ public JRJdbcQueryExecuter(JasperReportsContext jasperReportsContext, JRDataset dataset, Map<String, ? extends JRValueParameter> parameters) { super(jasperReportsContext, dataset, parameters); connection = (Connection) getParameterValue(JRParameter.REPORT_CONNECTION); if (connection == null) { if (log.isWarnEnabled()) { log.warn("The supplied java.sql.Connection object is null."); } } else if (log.isDebugEnabled()) { try { DatabaseMetaData metaData = connection.getMetaData(); log.debug("DB is " + metaData.getDatabaseProductName() + " version " + metaData.getDatabaseProductVersion() + " (" + metaData.getDatabaseMajorVersion() + "/" + metaData.getDatabaseMinorVersion() + ")"); log.debug("driver is " + metaData.getDriverName() + " version " + metaData.getDriverVersion() + " (" + metaData.getDriverMajorVersion() + "/" + metaData.getDriverMinorVersion() + ")"); log.debug("jdbc " + metaData.getJDBCMajorVersion() + "/" + metaData.getJDBCMinorVersion()); log.debug("connection URL is " + metaData.getURL()); } catch (SQLException e) { log.debug("failed to read connection metadata", e); } } isCachedRowSet = getBooleanParameterOrProperty(JRJdbcQueryExecuterFactory.PROPERTY_CACHED_ROWSET, false); setTimeZone(); registerFunctions(); parseQuery(); }
From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java
public void logDatabaseAndDriverInfo(ConnectionManager connectionManager) throws SQLException { DatabaseMetaData databaseMetaData = connectionManager.getConnection().getMetaData(); LOG.info("Database Product name: {}", databaseMetaData.getDatabaseProductName()); LOG.info("Database product version: {}", databaseMetaData.getDatabaseProductVersion()); LOG.info("Driver name: {}", databaseMetaData.getDriverName()); LOG.info("Driver version: {}", databaseMetaData.getDriverVersion()); }
From source file:com.netspective.axiom.policy.AnsiDatabasePolicy.java
public void reverseEngineer(Writer writer, Connection conn, String catalog, String schemaPattern) throws IOException, SQLException { Map dataTypesMap = prepareJdbcTypeInfoMap(); DatabaseMetaData dbmd = conn.getMetaData(); TextUtils textUtils = TextUtils.getInstance(); writer.write("<?xml version=\"1.0\"?>\n\n"); writer.write("<!-- Reverse engineered by Axiom\n"); writer.write(" driver: " + dbmd.getDriverName() + "\n"); writer.write(" driver-version: " + dbmd.getDriverVersion() + "\n"); writer.write(" product: " + dbmd.getDatabaseProductName() + "\n"); writer.write(" product-version: " + dbmd.getDatabaseProductVersion() + "\n"); writer.write(" available catalogs:"); ResultSet rs = null;/* w w w. j a v a 2 s. co m*/ try { rs = dbmd.getCatalogs(); while (rs.next()) { writer.write(" " + rs.getObject(1).toString()); } } finally { if (rs != null) rs.close(); } writer.write("\n"); writer.write(" available schemas:"); try { rs = dbmd.getSchemas(); while (rs.next()) { writer.write(" " + rs.getObject(1).toString()); } } finally { if (rs != null) rs.close(); } writer.write("\n"); writer.write("-->\n\n"); writer.write("<component xmlns:xdm=\"http://www.netspective.org/Framework/Commons/XMLDataModel\">\n"); writer.write(" <xdm:include resource=\"com/netspective/axiom/conf/axiom.xml\"/>\n"); writer.write(" <schema name=\"" + catalog + "." + schemaPattern + "\">\n"); Map dbmdTypeInfoByName = new HashMap(); Map dbmdTypeInfoByJdbcType = new HashMap(); ResultSet typesRS = null; try { typesRS = dbmd.getTypeInfo(); while (typesRS.next()) { int colCount = typesRS.getMetaData().getColumnCount(); Object[] typeInfo = new Object[colCount]; for (int i = 1; i <= colCount; i++) typeInfo[i - 1] = typesRS.getObject(i); dbmdTypeInfoByName.put(typesRS.getString(1), typeInfo); dbmdTypeInfoByJdbcType.put(new Integer(typesRS.getInt(2)), typeInfo); } } finally { if (typesRS != null) typesRS.close(); } ResultSet tables = null; try { tables = dbmd.getTables(catalog, schemaPattern, null, new String[] { "TABLE" }); while (tables.next()) { String tableNameOrig = tables.getString(3); String tableName = textUtils.fixupTableNameCase(tableNameOrig); writer.write(" <table name=\"" + tableName + "\">\n"); Map primaryKeys = new HashMap(); ResultSet pkRS = null; try { pkRS = dbmd.getPrimaryKeys(null, null, tableNameOrig); while (pkRS.next()) { primaryKeys.put(pkRS.getString(4), pkRS.getString(5)); } } catch (Exception e) { // driver may not support this function } finally { if (pkRS != null) pkRS.close(); } Map fKeys = new HashMap(); ResultSet fkRS = null; try { fkRS = dbmd.getImportedKeys(null, null, tableNameOrig); while (fkRS.next()) { fKeys.put(fkRS.getString(8), textUtils.fixupTableNameCase(fkRS.getString(3)) + "." + fkRS.getString(4).toLowerCase()); } } catch (Exception e) { // driver may not support this function } finally { if (fkRS != null) fkRS.close(); } // we keep track of processed columns so we don't duplicate them in the XML Set processedColsMap = new HashSet(); ResultSet columns = null; try { columns = dbmd.getColumns(null, null, tableNameOrig, null); while (columns.next()) { String columnNameOrig = columns.getString(4); if (processedColsMap.contains(columnNameOrig)) continue; processedColsMap.add(columnNameOrig); String columnName = columnNameOrig.toLowerCase(); writer.write(" <column name=\"" + columnName + "\""); try { if (fKeys.containsKey(columnNameOrig)) writer.write(" lookup-ref=\"" + fKeys.get(columnNameOrig) + "\""); else { short jdbcType = columns.getShort(5); String dataType = (String) dataTypesMap.get(new Integer(jdbcType)); if (dataType == null) dataType = Short.toString(jdbcType); writer.write(" type=\"" + dataType + "\""); } if (primaryKeys.containsKey(columnNameOrig)) writer.write(" primary-key=\"yes\""); if (columns.getString(18).equals("NO")) writer.write(" required=\"yes\""); String defaultValue = columns.getString(13); if (defaultValue != null) writer.write(" default=\"" + defaultValue + "\""); String remarks = columns.getString(12); if (remarks != null) writer.write(" descr=\"" + remarks + "\""); } catch (Exception e) { } writer.write("/>\n"); } } finally { if (columns != null) columns.close(); } writer.write(" </table>\n"); } } finally { tables.close(); } writer.write(" </schema>\n"); writer.write("</component>"); }
From source file:com.aurel.track.ApplicationStarter.java
private void printSystemInfo() { LOGGER.info("Java: " + System.getProperty("java.vendor") + " " + System.getProperty("java.version")); LOGGER.info("Operating System: " + System.getProperty("os.name") + " " + System.getProperty("os.arch")); Locale loc = Locale.getDefault(); LOGGER.info("Default locale: " + loc.getDisplayName()); ServletContext application = ApplicationBean.getInstance().getServletContext(); try {/*from w ww .j a v a2s .c om*/ LOGGER.info("Servlet real path: " + application.getRealPath(File.separator)); } catch (Exception ex) { LOGGER.error("Error trying to obtain getRealPath()"); } LOGGER.info("Servlet container: " + application.getServerInfo()); Connection conn = null; try { PropertiesConfiguration pc = ApplicationBean.getInstance().getDbConfig(); LOGGER.info("Configured database type: " + pc.getProperty("torque.database.track.adapter")); LOGGER.info( "Configured database driver: " + pc.getProperty("torque.dsfactory.track.connection.driver")); LOGGER.info("Configured JDBC URL: " + pc.getProperty("torque.dsfactory.track.connection.url")); conn = Torque.getConnection(BaseTSitePeer.DATABASE_NAME); DatabaseMetaData dbm = conn.getMetaData(); LOGGER.info("Database type: " + dbm.getDatabaseProductName() + " " + dbm.getDatabaseProductVersion()); LOGGER.info("Driver info: " + dbm.getDriverName() + " " + dbm.getDriverVersion()); Statement stmt = conn.createStatement(); Date d1 = new Date(); stmt.executeQuery("SELECT * FROM TSTATE"); Date d2 = new Date(); stmt.close(); LOGGER.info("Database test query done in " + (d2.getTime() - d1.getTime()) + " milliseconds "); } catch (Exception e) { System.err.println("Problem retrieving meta data"); LOGGER.error("Problem retrieving meta data"); } finally { if (conn != null) { Torque.closeConnection(conn); } } }
From source file:com.jaxio.celerio.configuration.database.support.MetadataExtractor.java
private DatabaseInfo extractDatabaseInfo(DatabaseMetaData databaseMetaData) { DatabaseInfo database = new DatabaseInfo(); // database/* w w w. j a va 2 s . co m*/ try { database.setDatabaseProductName(databaseMetaData.getDatabaseProductName()); } catch (Exception e) { /* ignore */ } try { database.setDatabaseProductVersion(databaseMetaData.getDatabaseProductVersion()); } catch (Exception e) { /* ignore */ } try { database.setDatabaseMajorVersion(databaseMetaData.getDatabaseMajorVersion()); } catch (Exception e) { /* ignore */ } try { database.setDatabaseMinorVersion(databaseMetaData.getDatabaseMinorVersion()); } catch (Exception e) { /* ignore */ } // driver try { database.setDriverName(databaseMetaData.getDriverName()); } catch (Exception e) { /* ignore */ } try { database.setDriverVersion(databaseMetaData.getDriverVersion()); } catch (Exception e) { /* ignore */ } try { database.setDriverMajorVersion(databaseMetaData.getDriverMajorVersion()); } catch (Exception e) { /* ignore */ } try { database.setDriverMinorVersion(databaseMetaData.getDriverMinorVersion()); } catch (Exception e) { /* ignore */ } return database; }
From source file:net.hydromatic.optiq.test.JdbcTest.java
/** * Make sure that the properties look sane. *//*from w ww . ja v a2 s .c om*/ @Test public void testVersion() throws ClassNotFoundException, SQLException { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); final DatabaseMetaData metaData = optiqConnection.getMetaData(); assertEquals("Optiq JDBC Driver", metaData.getDriverName()); final String driverVersion = metaData.getDriverVersion(); final int driverMajorVersion = metaData.getDriverMajorVersion(); final int driverMinorVersion = metaData.getDriverMinorVersion(); assertEquals(0, driverMajorVersion); assertEquals(4, driverMinorVersion); assertEquals("Optiq", metaData.getDatabaseProductName()); final String databaseProductVersion = metaData.getDatabaseProductVersion(); final int databaseMajorVersion = metaData.getDatabaseMajorVersion(); assertEquals(driverMajorVersion, databaseMajorVersion); final int databaseMinorVersion = metaData.getDatabaseMinorVersion(); assertEquals(driverMinorVersion, databaseMinorVersion); // Check how version is composed of major and minor version. Note that // version is stored in pom.xml; major and minor version are // stored in net-hydromatic-optiq-jdbc.properties. if (!driverVersion.endsWith("-SNAPSHOT")) { assertTrue(driverVersion.startsWith("0.")); String[] split = driverVersion.split("\\."); assertTrue(split.length >= 2); assertTrue(driverVersion.startsWith(driverMajorVersion + "." + driverMinorVersion + ".")); } if (!databaseProductVersion.endsWith("-SNAPSHOT")) { assertTrue(databaseProductVersion.startsWith("0.")); String[] split = databaseProductVersion.split("\\."); assertTrue(split.length >= 2); assertTrue(databaseProductVersion.startsWith(databaseMajorVersion + "." + databaseMinorVersion + ".")); } connection.close(); }
From source file:net.sf.jasperreports.engine.query.OracleProcedureCallHandler.java
protected boolean isDataDirectDriver() { Connection connection;//from www . j a v a 2s . c o m try { connection = statement.getConnection(); } catch (SQLException e) { log.error("Failure while detecting driver", e); return false; } DatabaseMetaData metaData = null; try { metaData = connection.getMetaData(); } catch (SQLException e) { log.error("Failure while detecting driver", e); } String connectionURL = null; if (metaData != null) { try { connectionURL = metaData.getURL(); } catch (SQLException e) { log.error("Failure while detecting driver", e); } } if (connectionURL != null) { if (connectionURL.contains(URL_DATADIRECT) || connectionURL.contains(URL_TIBCO)) { return true; } if (connectionURL.contains(URL_ORACLE)) { return false; } } if (ORACLE_CONNECTION_CLASS != null) { try { if (connection.isWrapperFor(ORACLE_CONNECTION_CLASS)) { return false; } } catch (SQLException e) { log.error("Failure while detecting driver", e); } } if (metaData != null) { try { String driverName = metaData.getDriverName(); if (driverName.equals(DRIVER_NAME_ORACLE)) { return false; } if (driverName.equals(DRIVER_NAME_DATADIRECT)) { return true; } } catch (SQLException e) { log.error("Failure while detecting driver", e); } } //fallback to Oracle return false; }
From source file:mondrian.spi.impl.JdbcDialectImpl.java
protected Set<List<Integer>> deduceSupportedResultSetStyles(DatabaseMetaData databaseMetaData) { Set<List<Integer>> supports = new HashSet<List<Integer>>(); for (int type : RESULT_SET_TYPE_VALUES) { for (int concurrency : CONCURRENCY_VALUES) { try { if (databaseMetaData.supportsResultSetConcurrency(type, concurrency)) { String driverName = databaseMetaData.getDriverName(); if (type != ResultSet.TYPE_FORWARD_ONLY && driverName.equals("JDBC-ODBC Bridge (odbcjt32.dll)")) { // In JDK 1.6, the Jdbc-Odbc bridge announces // that it can handle TYPE_SCROLL_INSENSITIVE // but it does so by generating a 'COUNT(*)' // query, and this query is invalid if the query // contains a single-quote. So, override the // driver. continue; }/*from w ww . ja v a2 s.c om*/ supports.add(new ArrayList<Integer>(Arrays.asList(type, concurrency))); } } catch (SQLException e) { // DB2 throws "com.ibm.db2.jcc.b.SqlException: Unknown type // or Concurrency" for certain values of type/concurrency. // No harm in interpreting all such exceptions as 'this // database does not support this type/concurrency // combination'. Util.discard(e); } } } return supports; }
From source file:org.metis.pull.WdsResourceBean.java
/** * Called by Spring after all of this bean's properties have been set. *///from w ww . ja va2 s .c o m public void afterPropertiesSet() throws Exception { // log info for the jdbc driver being used // this will also attempt to open connection // with jdbc driver try { Connection con = getDataSource().getConnection(); if (con != null) { DatabaseMetaData dbmd = con.getMetaData(); setDbConnectionAcquired(true); if (dbmd != null) { setDriverName(dbmd.getDriverName().trim().toLowerCase()); isOracle = (getDriverName() != null && getDriverName().indexOf(ORACLE_STR) >= 0) ? true : false; LOG.info(getBeanName() + ":Is Oracle JDBC Driver = " + isOracle); LOG.info(getBeanName() + ":JDBC Driver name = " + getDriverName()); LOG.info(getBeanName() + ":JDBC Driver version = " + dbmd.getDriverVersion().trim()); LOG.info(getBeanName() + ":JDBC Driver product name = " + dbmd.getDatabaseProductName().trim()); LOG.info(getBeanName() + ":JDBC Driver database product version = " + dbmd.getDatabaseProductVersion().trim()); con.close(); } else { LOG.info(getBeanName() + ": Unable to get JDBC driver meta data"); } } else { LOG.info(getBeanName() + ": Unable to get JDBC connection"); } } catch (SQLException exc) { LOG.error(getBeanName() + ": got this exception when trying to " + "get driver meta data: " + exc.toString()); LOG.error(getBeanName() + ": exception stack trace follows:"); dumpStackTrace(exc.getStackTrace()); LOG.error(getBeanName() + ": Caused by " + exc.getCause().toString()); LOG.error(getBeanName() + ": causing exception stack trace follows:"); dumpStackTrace(exc.getCause().getStackTrace()); } // bean must be assigned a JDBC DataSource if (getDataSource() == null) { throw new Exception( getBeanName() + ".afterPropertiesSet: this bean has not been " + "assigned a JDBC DataSource"); } // do some validation if (getSqls4Get() == null && getSqls4Put() == null && getSqls4Post() == null && getSqls4Delete() == null) { throw new Exception("At least one of the WdsResourceBean's http methods has " + "not been assigned a SQL statement"); } // create and validate the different SQL statements if (getSqls4Get() != null) { sqlStmnts4Get = new ArrayList<SqlStmnt>(); for (String sql : getSqls4Get()) { SqlStmnt stmt = getSQLStmnt(this, sql, getJdbcTemplate()); if (stmt.isEqual(sqlStmnts4Get)) { throw new Exception("Injected SQL statements for GET are not distinct"); } sqlStmnts4Get.add(stmt); } if (LOG.isDebugEnabled()) { for (SqlStmnt sqlstmnt : sqlStmnts4Get) { LOG.debug(getBeanName() + ": SQL for GET = " + sqlstmnt.getOriginal()); LOG.debug(getBeanName() + ": Parameterized SQL for GET = " + sqlstmnt.getPrepared()); } } allowedMethodsRsp += "GET "; } if (getSqls4Put() != null) { sqlStmnts4Put = new ArrayList<SqlStmnt>(); for (String sql : getSqls4Put()) { SqlStmnt stmt = getSQLStmnt(this, sql, getJdbcTemplate()); if (stmt.isEqual(sqlStmnts4Put)) { throw new Exception("Injected SQL statements for PUT are not distinct"); } sqlStmnts4Put.add(stmt); } if (LOG.isDebugEnabled()) { for (SqlStmnt sqlstmnt : sqlStmnts4Put) { LOG.debug(getBeanName() + ": SQL for PUT = " + sqlstmnt.getOriginal()); LOG.debug(getBeanName() + ": Parameterized SQL for PUT = " + sqlstmnt.getPrepared()); } } allowedMethodsRsp += "PUT "; } if (getSqls4Post() != null) { sqlStmnts4Post = new ArrayList<SqlStmnt>(); for (String sql : getSqls4Post()) { SqlStmnt stmt = getSQLStmnt(this, sql, getJdbcTemplate()); if (stmt.isEqual(sqlStmnts4Post)) { throw new Exception("Injected SQL statements for POST are not distinct"); } sqlStmnts4Post.add(stmt); } if (LOG.isDebugEnabled()) { for (SqlStmnt sqlstmnt : sqlStmnts4Post) { LOG.debug(getBeanName() + ": SQL for POST = " + sqlstmnt.getOriginal()); LOG.debug(getBeanName() + ": Parameterized SQL for POST = " + sqlstmnt.getPrepared()); } } allowedMethodsRsp += "POST "; } if (getSqls4Delete() != null) { sqlStmnts4Delete = new ArrayList<SqlStmnt>(); for (String sql : getSqls4Delete()) { SqlStmnt stmt = getSQLStmnt(this, sql, getJdbcTemplate()); if (stmt.isEqual(sqlStmnts4Delete)) { throw new Exception("Injected SQL statements for DELETE are not distinct"); } sqlStmnts4Delete.add(stmt); } if (LOG.isDebugEnabled()) { for (SqlStmnt sqlstmnt : sqlStmnts4Delete) { LOG.debug(getBeanName() + ": SQL for DELETE = " + sqlstmnt.getOriginal()); LOG.debug(getBeanName() + ": Parameterized SQL for DELETE = " + sqlstmnt.getPrepared()); } } allowedMethodsRsp += "DELETE"; } LOG.debug(getBeanName() + ": allowedMethodsRsp string = " + allowedMethodsRsp); // tell our parent what methods this RDB will support setSupportedMethods(allowedMethodsRsp.split(SPACE_CHR_STR)); if (LOG.isDebugEnabled() && getAllowedAgents() != null) { if (!getAllowedAgents().isEmpty()) { LOG.debug(getBeanName() + ": agents allowed = " + getAllowedAgents()); } else { LOG.debug(getBeanName() + ": agents not allowed = " + getNotAllowedAgents()); } } }