List of usage examples for java.sql DatabaseMetaData getDatabaseProductName
String getDatabaseProductName() throws SQLException;
From source file:com.aionemu.commons.database.DatabaseFactory.java
public synchronized static void init(String configFilePath) { if (dataSource != null) { return;//from w w w . ja va2 s . c om } if (!configFilePath.equals("")) { try { Properties dbProps = PropertiesUtils.load(configFilePath); ConfigurableProcessor.process(DatabaseConfig.class, dbProps); } catch (IOException ex) { log.fatal("Cannot load database config", ex); } } try { DatabaseConfig.DATABASE_DRIVER.newInstance(); } catch (Exception e) { log.fatal("Error obtaining DB driver", e); throw new Error("DB Driver doesnt exist!"); } connectionPool = new GenericObjectPool(); if (DatabaseConfig.DATABASE_CONNECTIONS_MIN > DatabaseConfig.DATABASE_CONNECTIONS_MAX) { log.error("Please check your database configuration. Minimum amount of connections is > maximum"); DatabaseConfig.DATABASE_CONNECTIONS_MAX = DatabaseConfig.DATABASE_CONNECTIONS_MIN; } connectionPool.setMaxIdle(DatabaseConfig.DATABASE_CONNECTIONS_MIN); connectionPool.setMaxActive(DatabaseConfig.DATABASE_CONNECTIONS_MAX); /* test if connection is still valid before returning */ //connectionPool.setTestOnBorrow(true); try { dataSource = setupDataSource(); Connection c = getConnection(); DatabaseMetaData dmd = c.getMetaData(); databaseName = dmd.getDatabaseProductName(); databaseMajorVersion = dmd.getDatabaseMajorVersion(); databaseMinorVersion = dmd.getDatabaseMinorVersion(); c.close(); } catch (Exception e) { log.fatal("Error with connection string: " + DatabaseConfig.DATABASE_URL, e); throw new Error("DatabaseFactory not initialized!"); } log.info("Successfully connected to database"); }
From source file:com.glaf.core.jdbc.DBConnectionFactory.java
public static String getDatabaseType(Connection connection) { if (connection != null) { String databaseProductName = null; try {/*from www . ja va2 s .c o m*/ DatabaseMetaData databaseMetaData = connection.getMetaData(); databaseProductName = databaseMetaData.getDatabaseProductName(); } catch (SQLException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } String dbType = databaseTypeMappings.getProperty(databaseProductName); if (dbType == null) { throw new RuntimeException( "couldn't deduct database type from database product name '" + databaseProductName + "'"); } return dbType; } return null; }
From source file:com.jagornet.dhcp.db.DbSchemaManager.java
/** * Validate schema./*from w w w . j a va 2 s . c o m*/ * * @param dataSource the data source * * @throws SQLException if there is a problem with the database * @throws IOExcpetion if there is a problem reading the schema file * * returns true if database was created, false otherwise */ public static boolean validateSchema(DataSource dataSource, String schemaFilename, int schemaVersion) throws SQLException, IOException { boolean schemaCreated = false; List<String> tableNames = new ArrayList<String>(); Connection conn = dataSource.getConnection(); DatabaseMetaData dbMetaData = conn.getMetaData(); log.info("JDBC Connection Info:\n" + "url = " + dbMetaData.getURL() + "\n" + "database = " + dbMetaData.getDatabaseProductName() + " " + dbMetaData.getDatabaseProductVersion() + "\n" + "driver = " + dbMetaData.getDriverName() + " " + dbMetaData.getDriverVersion()); String[] types = { "TABLE" }; ResultSet rs = dbMetaData.getTables(null, null, "%", types); if (rs.next()) { tableNames.add(rs.getString("TABLE_NAME")); } else { createSchema(dataSource, schemaFilename); dbMetaData = conn.getMetaData(); rs = dbMetaData.getTables(null, null, "%", types); schemaCreated = true; } while (rs.next()) { tableNames.add(rs.getString("TABLE_NAME")); } String[] schemaTableNames; if (schemaVersion <= 1) { schemaTableNames = TABLE_NAMES; } else { schemaTableNames = TABLE_NAMES_V2; } if (tableNames.size() == schemaTableNames.length) { for (int i = 0; i < schemaTableNames.length; i++) { if (!tableNames.contains(schemaTableNames[i])) { throw new IllegalStateException("Invalid database schema: unknown tables"); } } } else { throw new IllegalStateException("Invalid database schema: wrong number of tables"); } return schemaCreated; }
From source file:com.predic8.membrane.core.interceptor.statistics.util.JDBCUtil.java
public static boolean isOracleDatabase(DatabaseMetaData metaData) throws SQLException { return metaData.getDatabaseProductName().startsWith("Oracle"); }
From source file:com.predic8.membrane.core.interceptor.statistics.util.JDBCUtil.java
public static boolean isMySQLDatabase(DatabaseMetaData metaData) throws SQLException { return metaData.getDatabaseProductName().startsWith("MySQL"); }
From source file:com.predic8.membrane.core.interceptor.statistics.util.JDBCUtil.java
public static boolean isDerbyDatabase(DatabaseMetaData metaData) throws SQLException { return metaData.getDatabaseProductName().startsWith("Derby"); }
From source file:com.aurel.track.admin.server.status.ServerStatusBL.java
private static void loadDatabaseInfo(ServerStatusTO serverStatusTO) { Connection conn = null;/* w w w. j a v a2s. co m*/ try { conn = Torque.getConnection(BaseTSitePeer.DATABASE_NAME); DatabaseMetaData dbm = conn.getMetaData(); serverStatusTO.setDatabase(dbm.getDatabaseProductName() + " " + dbm.getDatabaseProductVersion()); serverStatusTO.setJdbcDriver(dbm.getDriverName() + " " + dbm.getDriverVersion()); serverStatusTO.setJdbcUrl(dbm.getURL()); } catch (Exception e) { LOGGER.error("Problem retrieving database meta data: " + e.getMessage()); } finally { if (conn != null) { Torque.closeConnection(conn); } } Double ping = loadPing(); serverStatusTO.setPingTime(ping.toString() + " ms"); }
From source file:org.wso2.carbon.analytics.datasource.rdbms.RDBMSUtils.java
public static Map<String, Object> lookupDatabaseInfo(DataSource ds) throws AnalyticsException { Connection conn = null;/* w w w . j a va 2 s . co m*/ try { conn = ds.getConnection(); DatabaseMetaData dmd = conn.getMetaData(); Map<String, Object> result = new HashMap<String, Object>(); result.put(DATABASE_PRODUCT_NAME, dmd.getDatabaseProductName()); result.put(VERSION, Double.parseDouble(dmd.getDatabaseMajorVersion() + "." + dmd.getDatabaseMinorVersion())); return result; } catch (SQLException e) { throw new AnalyticsException("Error in looking up database type: " + e.getMessage(), e); } finally { RDBMSUtils.cleanupConnection(null, null, conn); } }
From source file:org.wso2.carbon.event.simulator.admin.internal.util.EventSimulatorDataSourceInfo.java
/** * Validates database table information Construct all the queries and assign to executionInfo instance * * @param tableAndAttributeMappingJsonObj * JSONObject which contains dataSource, event stream, configuration name, * table name, delay between events in milliseconds, * table columns and mapping stream attributes and types *//* www.j av a 2s. c o m*/ public static ExecutionInfo getInitializedDatabaseExecutionInfo(JSONObject tableAndAttributeMappingJsonObj) throws AxisFault { Connection con; String dbName; Statement stmt; populateJaxbMappings(); ExecutionInfo executionInfo = new ExecutionInfo(); String dataSourceName; try { dataSourceName = tableAndAttributeMappingJsonObj.getString(EventSimulatorConstant.DATA_SOURCE_NAME); String tableName = tableAndAttributeMappingJsonObj.getString(EventSimulatorConstant.TABLE_NAME); try { CarbonDataSource carbonDataSource = EventSimulatorAdminvalueHolder.getDataSourceService() .getDataSource(dataSourceName); if (carbonDataSource != null) { executionInfo.setDatasource((DataSource) carbonDataSource.getDSObject()); } try { con = executionInfo.getDatasource().getConnection(); DatabaseMetaData databaseMetaData = con.getMetaData(); dbName = databaseMetaData.getDatabaseProductName(); Map<String, String> elementMappings = dbTypeMappings.get(dbName.toLowerCase()); String isTableExistQuery = elementMappings.get("isTableExist").replace( EventSimulatorDataSourceConstants.GENERIC_RDBMS_ATTRIBUTE_TABLE_NAME, tableName); executionInfo.setPreparedTableExistenceCheckStatement(isTableExistQuery); try { stmt = con.createStatement(); stmt.executeQuery(executionInfo.getPreparedTableExistenceCheckStatement()); String getColumnsQuery = ""; boolean addedFirstColumn = false; JSONArray attributeColumnMappingArray = tableAndAttributeMappingJsonObj .getJSONArray(EventSimulatorConstant.DATABASE_COLUMNS_AND_STREAM_ATTRIBUTE_INFO); for (int i = 0; i < attributeColumnMappingArray.length(); i++) { JSONObject attributeAndMappingColumn = attributeColumnMappingArray.getJSONObject(i); if (!getColumnsQuery.contains( attributeAndMappingColumn.getString(EventSimulatorConstant.COLUMN_NAME))) { if (addedFirstColumn) { getColumnsQuery = getColumnsQuery + ","; } addedFirstColumn = true; getColumnsQuery = getColumnsQuery + attributeAndMappingColumn.getString(EventSimulatorConstant.COLUMN_NAME); } } String columnsDataTypeQuery = elementMappings.get("selectAllColumnsDataTypeInTable") .replace(EventSimulatorDataSourceConstants.GENERIC_RDBMS_ATTRIBUTE_TABLE_NAME, tableName); String selectQuery = elementMappings.get("selectFromTable") .replace(EventSimulatorDataSourceConstants.GENERIC_RDBMS_ATTRIBUTE_TABLE_NAME, tableName) .replace(EventSimulatorDataSourceConstants.GENERIC_RDBMS_ATTRIBUTE_COLUMNS, getColumnsQuery); executionInfo.setPreparedCheckTableColumnsDataTypeStatement(columnsDataTypeQuery); executionInfo.setPreparedSelectStatement(selectQuery); int columnAndDataTypeCount = 0; columnsDataTypeQuery = executionInfo.getPreparedCheckTableColumnsDataTypeStatement(); //to check validity of entered columns ResultSet rs = stmt.executeQuery(columnsDataTypeQuery); while (rs.next()) { String tableVariable = rs.getString(1); for (int j = 0; j < attributeColumnMappingArray.length(); j++) { JSONObject mappingAttributeAndColumn = attributeColumnMappingArray.getJSONObject(j); if (mappingAttributeAndColumn.getString(EventSimulatorConstant.COLUMN_NAME) .equalsIgnoreCase(tableVariable)) { columnAndDataTypeCount++; } } } if (columnAndDataTypeCount < attributeColumnMappingArray.length()) { log.error("Entered Column name(s) are nt valid in " + tableName); throw new AxisFault("Entered Column name(s) are nt valid in " + tableName); } rs = stmt.executeQuery(executionInfo.getPreparedSelectStatement()); if (!rs.next()) { log.error(tableName + " table does not contain data"); throw new AxisFault(tableName + " table does not contain data"); } cleanupConnections(stmt, con); } catch (SQLException e) { log.error(tableName + " table does not exist or no data", e); throw new AxisFault(tableName + " table does not exist or no data", e); } } catch (SQLException e) { log.error("Exception when getting connection string for : " + dataSourceName, e); throw new AxisFault("Exception when getting connection string for : " + dataSourceName, e); } } catch (DataSourceException e) { log.error("There is no any data source found named: " + dataSourceName, e); throw new AxisFault("There is no any data source found named: " + dataSourceName, e); } } catch (JSONException e) { log.error("Created JSON formatted string with attribute mapping information is not valid", e); throw new AxisFault("Created JSON formatted string with attribute mapping information is not valid", e); } return executionInfo; }
From source file:org.snaker.engine.access.jdbc.JdbcHelper.java
/** * ???//ww w . j av a2 s. c om * @param conn ? * @return * @throws Exception */ public static String getDatabaseType(Connection conn) throws Exception { DatabaseMetaData databaseMetaData = conn.getMetaData(); String databaseProductName = databaseMetaData.getDatabaseProductName(); return databaseTypeMappings.getProperty(databaseProductName); }