Java 8 DatabaseMetaData - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DatabaseMetaData class.

This code example shows how to use the following methods:getDatabaseProductName, getDatabaseProductVersion, getTables, getUserName, storesLowerCaseIdentifiers
 
	@Test
	public void testNoSuchTable() throws Exception {
		ResultSet resultSet = mock(ResultSet.class);
		given(resultSet.next()).willReturn(false);
		given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
		given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
		given(databaseMetaData.getDatabaseProductVersion()).willReturn("1.0");
		given(databaseMetaData.getUserName()).willReturn("me");
		given(databaseMetaData.storesLowerCaseIdentifiers()).willReturn(true);
		given(databaseMetaData.getTables(null, null, "x", null)).willReturn(resultSet);
 
		SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource).withTableName("x");
		// Shouldn't succeed in inserting into table which doesn't exist 
		thrown.expect(InvalidDataAccessApiUsageException.class);
		try { 
			insert.execute(new HashMap<String, Object>());
		} 
		finally { 
			verify(resultSet).close();
8
CodeRank
This code example shows how to use the following methods:getDriverName, getDriverVersion, supportsGetGeneratedKeys
		super(databaseMetaData);
	} 
 
	@Override 
	public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
		super.initializeWithMetaData(databaseMetaData);
		if (!databaseMetaData.supportsGetGeneratedKeys()) {
			logger.warn("Overriding supportsGetGeneratedKeys from DatabaseMetaData to 'true'; it was reported as " + 
					"'false' by " + databaseMetaData.getDriverName() + " " + databaseMetaData.getDriverVersion());
			supportsGeneratedKeysOverride = true;
		} 
	} 
 
	@Override 
	public boolean isGetGeneratedKeysSupported() { 
		boolean derbysAnswer = super.isGetGeneratedKeysSupported();
		if (!derbysAnswer) {
			return supportsGeneratedKeysOverride;
		} 
		return derbysAnswer;
8
CodeRank
This code example shows how to use the following methods:storesLowerCaseIdentifiers, storesUpperCaseIdentifiers, supportsCatalogsInProcedureCalls, supportsSchemasInProcedureCalls
 
 
	@Override 
	public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
		try { 
			setSupportsCatalogsInProcedureCalls(databaseMetaData.supportsCatalogsInProcedureCalls());
		} 
		catch (SQLException se) {
			logger.debug("Error retrieving 'DatabaseMetaData.supportsCatalogsInProcedureCalls' - " + se.getMessage());
		} 
		try { 
			setSupportsSchemasInProcedureCalls(databaseMetaData.supportsSchemasInProcedureCalls());
		} 
		catch (SQLException se) {
			logger.debug("Error retrieving 'DatabaseMetaData.supportsSchemasInProcedureCalls' - " + se.getMessage());
		} 
		try { 
			setStoresUpperCaseIdentifiers(databaseMetaData.storesUpperCaseIdentifiers());
		} 
		catch (SQLException se) {

8
CodeRank
This code example shows how to use the following methods:getDatabaseProductName, getProcedureColumns, getProcedures, getUserName, storesUpperCaseIdentifiers
	} 
 
	private void initializeAddInvoiceWithMetaData(boolean isFunction) throws SQLException {
		ResultSet proceduresResultSet = mock(ResultSet.class);
		ResultSet procedureColumnsResultSet = mock(ResultSet.class);
		given(databaseMetaData.getDatabaseProductName()).willReturn("Oracle");
		given(databaseMetaData.getUserName()).willReturn("ME");
		given(databaseMetaData.storesUpperCaseIdentifiers()).willReturn(true);
		given(databaseMetaData.getProcedures("", "ME", "ADD_INVOICE")).willReturn(proceduresResultSet);
		given(databaseMetaData.getProcedureColumns("", "ME", "ADD_INVOICE", null)).willReturn(procedureColumnsResultSet);
 
		given(proceduresResultSet.next()).willReturn(true, false);
		given(proceduresResultSet.getString("PROCEDURE_NAME")).willReturn("add_invoice");
 
		given(procedureColumnsResultSet.next()).willReturn(true, true, true, false);
		given(procedureColumnsResultSet.getInt("DATA_TYPE")).willReturn(4);
		if (isFunction) {
			given(procedureColumnsResultSet.getString("COLUMN_NAME")).willReturn(null,"amount", "custid");
			given(procedureColumnsResultSet.getInt("COLUMN_TYPE")).willReturn(5, 1, 1);
			given(connection.prepareCall("{? = call ADD_INVOICE(?, ?)}")).willReturn(callableStatement);
8
CodeRank
This code example shows how to use the following methods:getDatabaseProductName, getUserName, storesLowerCaseIdentifiers
 
	@Test
	public void testMatchParameterValuesAndSqlInOutParameters() throws Exception {
		final String TABLE = "customers";
		final String USER = "me";
		given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
		given(databaseMetaData.getUserName()).willReturn(USER);
		given(databaseMetaData.storesLowerCaseIdentifiers()).willReturn(true);
 
		List<SqlParameter> parameters = new ArrayList<SqlParameter>();
		parameters.add(new SqlParameter("id", Types.NUMERIC));
		parameters.add(new SqlInOutParameter("name", Types.NUMERIC));
		parameters.add(new SqlOutParameter("customer_no", Types.NUMERIC));
 
		MapSqlParameterSource parameterSource = new MapSqlParameterSource();
		parameterSource.addValue("id", 1);
		parameterSource.addValue("name", "Sven");
		parameterSource.addValue("customer_no", "12345XYZ");
 
		context.setProcedureName(TABLE);
7
CodeRank
This code example shows how to use the following methods:getDriverName, getDriverVersion
        return; 
      } 
      DatabaseMetaData meta = connection.getMetaData();
      DBUtil util = new DBUtil();
      util.setContext(getContext());
      supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
      supportsBatchUpdates = util.supportsBatchUpdates(meta);
      dialectCode = DBUtil.discoverSQLDialect(meta);
      addInfo("Driver name="+meta.getDriverName());
      addInfo("Driver version="+meta.getDriverVersion());
      addInfo("supportsGetGeneratedKeys="+supportsGetGeneratedKeys);
       
    } catch (SQLException se) {
      addWarn("Could not discover the dialect to use.", se);
    } finally { 
      DBHelper.closeConnection(connection);
    } 
  } 
 
  /** 
7
CodeRank
This code example shows how to use the following methods:supportsResultSetType
    } 
 
    @KnownFailure("not supported") 
    public void test_supportsResultSetTypeI() throws SQLException { 
        assertTrue("database supports TYPE_FORWARD_ONLY type", meta
                .supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY));
        assertFalse("database doesn't support TYPE_SCROLL_INSENSITIVE type",
                meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE));
        assertFalse("database supports TYPE_SCROLL_SENSITIVE type", meta
                .supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE));
        assertFalse("database supports unknown type", meta
                .supportsResultSetType(100));
    } 
 
    public void test_supportsSavepoints() throws SQLException { 
        assertFalse(meta.supportsSavepoints());
    } 
 
    public void test_supportsSchemasInDataManipulation() throws SQLException { 
        assertFalse(meta.supportsSchemasInDataManipulation());
7
CodeRank
This code example shows how to use the following methods:getDatabaseMajorVersion, getDatabaseMinorVersion, getDatabaseProductName
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            if (databaseMetaData == null) {
                throw new FlywayException("Unable to read database metadata while it is null!"); 
            } 
 
            String databaseProductName = databaseMetaData.getDatabaseProductName();
            if (databaseProductName == null) {
                throw new FlywayException("Unable to determine database. Product name is null."); 
            } 
 
            int databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
            int databaseMinorVersion = databaseMetaData.getDatabaseMinorVersion();
 
            return databaseProductName + " " + databaseMajorVersion + "." + databaseMinorVersion;
        } catch (SQLException e) {
            throw new FlywayException("Error while determining database product name", e);
        } 
    } 
 
	/** 
6
CodeRank
This code example shows how to use the following methods:getDatabaseProductName
 
		_connection = connection;
 
		DatabaseMetaData metaData = _connection.getMetaData();
 
		String productName = metaData.getDatabaseProductName();
 
		if (productName.equals("Microsoft SQL Server")) {
			_useUpgradeOptimizedPreparedStatementHandler = true;
		} 
		else { 
			_useUpgradeOptimizedPreparedStatementHandler = false;
		} 
	} 
 
	@Override 
	public Object invoke(Object proxy, Method method, Object[] arguments)
		throws Throwable {
 
		try { 
6
CodeRank
This code example shows how to use the following methods:getIdentifierQuoteString, storesLowerCaseIdentifiers, storesLowerCaseQuotedIdentifiers, storesUpperCaseIdentifiers, storesUpperCaseQuotedIdentifiers
See Code Examples for Java 8 DatabaseMetaData Methods: