Java Code Examples for java.sql.DatabaseMetaData#getTypeInfo()
The following examples show how to use
java.sql.DatabaseMetaData#getTypeInfo() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpatialFunctions.java From sis with Apache License 2.0 | 6 votes |
/** * Creates a new accessor to geospatial functions for the database described by given metadata. */ SpatialFunctions(final DatabaseMetaData metadata) throws SQLException { /* * Get information about whether byte are unsigned. * According JDBC specification, the rows shall be ordered by DATA_TYPE. * But the PostgreSQL driver 42.2.2 still provides rows in random order. */ boolean unsigned = true; try (ResultSet reflect = metadata.getTypeInfo()) { while (reflect.next()) { if (reflect.getInt(Reflection.DATA_TYPE) == Types.TINYINT) { unsigned = reflect.getBoolean(Reflection.UNSIGNED_ATTRIBUTE); if (unsigned) break; // Give precedence to "true" value. } } } isByteUnsigned = unsigned; /* * The library to use depends on the database implementation. * For now use the default library. */ library = null; }
Example 2
Source File: Show.java From jsqsh with Apache License 2.0 | 6 votes |
private ResultSet doTypes(Session session, Connection con, Options options) throws SQLException { if (options.arguments.size() != 1) { session.err.println("Use: \\show [-e] types"); return null; } if (options.essential) { options.columns = essentialTypesCols; } DatabaseMetaData meta = con.getMetaData(); return meta.getTypeInfo(); }
Example 3
Source File: MetaDataRegressionTest.java From Komondor with GNU General Public License v3.0 | 6 votes |
/** * Tests fix for BUG#21978216, GETTYPEINFO REPORT MAXIMUM PRECISION OF 255 FOR VARBINARY * * @throws Exception * if the test fails. */ public void testBug21978216() throws Exception { DatabaseMetaData meta = this.conn.getMetaData(); this.rs = meta.getTypeInfo(); while (this.rs.next()) { if (this.rs.getString("TYPE_NAME").equals("VARBINARY")) { if (versionMeetsMinimum(5, 0, 3)) { assertEquals(65535, this.rs.getInt("PRECISION")); } else { assertEquals(255, this.rs.getInt("PRECISION")); } } } }
Example 4
Source File: MetaDataRegressionTest.java From Komondor with GNU General Public License v3.0 | 6 votes |
/** * Tests fix for BUG#76187 (20675539), getTypeInfo report maximum precision of 255 for varchar. * * @throws Exception * if the test fails. */ public void testBug76187() throws Exception { DatabaseMetaData meta = this.conn.getMetaData(); this.rs = meta.getTypeInfo(); while (this.rs.next()) { if (this.rs.getString("TYPE_NAME").equals("VARCHAR")) { if (versionMeetsMinimum(5, 0, 3)) { assertEquals(65535, this.rs.getInt("PRECISION")); } else { assertEquals(255, this.rs.getInt("PRECISION")); } } } }
Example 5
Source File: MetaDataRegressionTest.java From r-course with MIT License | 6 votes |
/** * Tests fix for BUG#21978216, GETTYPEINFO REPORT MAXIMUM PRECISION OF 255 FOR VARBINARY * * @throws Exception * if the test fails. */ public void testBug21978216() throws Exception { DatabaseMetaData meta = this.conn.getMetaData(); this.rs = meta.getTypeInfo(); while (this.rs.next()) { if (this.rs.getString("TYPE_NAME").equals("VARBINARY")) { if (versionMeetsMinimum(5, 0, 3)) { assertEquals(65535, this.rs.getInt("PRECISION")); } else { assertEquals(255, this.rs.getInt("PRECISION")); } } } }
Example 6
Source File: Jdbc42DateTime.java From SimpleFlatMapper with MIT License | 5 votes |
public static void main(String[] args) throws SQLException { Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL); final DatabaseMetaData metaData = connection.getMetaData(); final String driverVersion = metaData.getDriverVersion(); System.out.println("driverVersion = " + driverVersion); System.out.println("jdbcVersion = " + metaData.getJDBCMajorVersion() + "." + metaData.getJDBCMinorVersion()); final String timeDateFunctions = metaData.getTimeDateFunctions(); System.out.println("timeDateFunctions = " + timeDateFunctions); ResultSet rs = metaData.getTypeInfo(); while(rs.next()) { for(int i = 0; i < rs.getMetaData().getColumnCount(); i++) { System.out.print(rs.getMetaData().getColumnName(i + 1) + " = " + rs.getObject(i + 1)); System.out.print(", "); } System.out.println(""); } final Statement statement = connection.createStatement(); final ResultSet resultSet = statement.executeQuery("SELECT current_timestamp"); while (resultSet.next()) { final String columnTypeName = resultSet.getMetaData().getColumnTypeName(1); System.out.println("columnTypeName = " + columnTypeName); final Timestamp timestamp = resultSet.getTimestamp(1); System.out.println("resultSet = " + timestamp); final Object object = resultSet.getObject(1, OffsetDateTime.class); System.out.println("resultSet = " + object + "/" + object.getClass()); } }
Example 7
Source File: DatabaseStatusChecker.java From dubbox with Apache License 2.0 | 5 votes |
public Status check() { boolean ok; try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { ok = resultSet.next(); } finally { resultSet.close(); } if (message == null) { message = metaData.getURL() + " (" + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion() + ", " + getIsolation(metaData.getDefaultTransactionIsolation()) + ")"; } if (version == 0) { version = metaData.getDatabaseMajorVersion(); } } finally { connection.close(); } } catch (Throwable e) { logger.error(e.getMessage(), e); ok = false; } return new Status(! ok ? Status.Level.ERROR : (version < 5 ? Status.Level.WARN : Status.Level.OK), message); }
Example 8
Source File: JdbcDB.java From quark with Apache License 2.0 | 5 votes |
protected ImmutableMap<String, Integer> getTypes(Connection connection) throws SQLException { DatabaseMetaData databaseMetaData = connection.getMetaData(); ResultSet rs = databaseMetaData.getTypeInfo(); ImmutableMap.Builder<String, Integer> builder = new ImmutableMap.Builder<>(); while (rs.next()) { LOG.debug("Registering data type '" + rs.getString("TYPE_NAME") + "'"); builder.put(rs.getString("TYPE_NAME").toUpperCase(), rs.getInt("DATA_TYPE")); } return builder.build(); }
Example 9
Source File: ResultSetIT.java From snowflake-jdbc with Apache License 2.0 | 5 votes |
@Test public void testGetObjectOnDatabaseMetadataResultSet() throws SQLException { Connection connection = getConnection(); DatabaseMetaData databaseMetaData = connection.getMetaData(); ResultSet resultSet = databaseMetaData.getTypeInfo(); resultSet.next(); // SNOW-21375 "NULLABLE" Column is a SMALLINT TYPE assertEquals(DatabaseMetaData.typeNullable, resultSet.getObject("NULLABLE")); resultSet.close(); connection.close(); }
Example 10
Source File: DatabaseStatusChecker.java From dubbo3 with Apache License 2.0 | 5 votes |
public Status check() { boolean ok; try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { ok = resultSet.next(); } finally { resultSet.close(); } if (message == null) { message = metaData.getURL() + " (" + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion() + ", " + getIsolation(metaData.getDefaultTransactionIsolation()) + ")"; } if (version == 0) { version = metaData.getDatabaseMajorVersion(); } } finally { connection.close(); } } catch (Throwable e) { logger.error(e.getMessage(), e); ok = false; } return new Status(! ok ? Status.Level.ERROR : (version < 5 ? Status.Level.WARN : Status.Level.OK), message); }
Example 11
Source File: MetaResultSetTest.java From calcite-avatica with Apache License 2.0 | 5 votes |
@Test public void testGetTypeInfo() throws SQLException { DatabaseMetaData metadata = getDatabaseMetadata(); try (ResultSet rs = metadata.getTypeInfo()) { ResultSetMetaData rsMeta = rs.getMetaData(); assertEquals(18, rsMeta.getColumnCount()); assertColumn(rsMeta, 1, "TYPE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 2, "DATA_TYPE", Types.INTEGER, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 3, "PRECISION", Types.INTEGER, DatabaseMetaData.columnNullable); assertColumn(rsMeta, 4, "LITERAL_PREFIX", Types.VARCHAR, DatabaseMetaData.columnNullable); assertColumn(rsMeta, 5, "LITERAL_SUFFIX", Types.VARCHAR, DatabaseMetaData.columnNullable); assertColumn(rsMeta, 6, "CREATE_PARAMS", Types.VARCHAR, DatabaseMetaData.columnNullable); assertColumn(rsMeta, 7, "NULLABLE", Types.SMALLINT, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 8, "CASE_SENSITIVE", Types.BOOLEAN, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 9, "SEARCHABLE", Types.SMALLINT, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 10, "UNSIGNED_ATTRIBUTE", Types.BOOLEAN, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 11, "FIXED_PREC_SCALE", Types.BOOLEAN, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 12, "AUTO_INCREMENT", Types.BOOLEAN, DatabaseMetaData.columnNoNulls); assertColumn(rsMeta, 13, "LOCAL_TYPE_NAME", Types.VARCHAR, DatabaseMetaData.columnNullable); assertColumn(rsMeta, 14, "MINIMUM_SCALE", Types.SMALLINT, DatabaseMetaData.columnNullable); assertColumn(rsMeta, 15, "MAXIMUM_SCALE", Types.SMALLINT, DatabaseMetaData.columnNullable); assertColumn(rsMeta, 16, "SQL_DATA_TYPE", Types.INTEGER, DatabaseMetaData.columnNullableUnknown); assertColumn(rsMeta, 17, "SQL_DATETIME_SUB", Types.INTEGER, DatabaseMetaData.columnNullableUnknown); assertColumn(rsMeta, 18, "NUM_PREC_RADIX", Types.INTEGER, DatabaseMetaData.columnNullable); } }
Example 12
Source File: DatabaseStatusChecker.java From dubbox with Apache License 2.0 | 5 votes |
public Status check() { boolean ok; try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { ok = resultSet.next(); } finally { resultSet.close(); } if (message == null) { message = metaData.getURL() + " (" + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion() + ", " + getIsolation(metaData.getDefaultTransactionIsolation()) + ")"; } if (version == 0) { version = metaData.getDatabaseMajorVersion(); } } finally { connection.close(); } } catch (Throwable e) { logger.error(e.getMessage(), e); ok = false; } return new Status(! ok ? Status.Level.ERROR : (version < 5 ? Status.Level.WARN : Status.Level.OK), message); }
Example 13
Source File: DbConn.java From XBatis-Code-Generator with Apache License 2.0 | 5 votes |
private DatabaseMetaData printDatabaseMetaData() { DatabaseMetaData dbmd = null; ResultSet rs = null; try { dbmd = conn.getMetaData(); /* * 获取当前数据库的数据类型信息。返回18列数据,如下所示 */ rs = dbmd.getTypeInfo(); logger.info("===============================数据库支持的数据类型:"); while (rs.next()) { logger.info("类型名称【" + StringUtil.genLengthStr(rs.getString(1), 20) + "】SqlType【" + StringUtil.genLengthStr(rs.getString(2), 5) + "】最大精度【" + StringUtil.genLengthStr(rs.getString(3), 10) + "】"); } /* 获取数据库信息 */ String dbType = dbmd.getDatabaseProductName(); // 获取当前数据库是什么数据库。如mysql等。返回的是字符串。 String dbVersion = dbmd.getDatabaseProductVersion(); // 获得数据库的版本。返回的字符串。 String driverName = dbmd.getDriverName(); // 获得驱动程序的名称。返回字符串。 String driverVersion = dbmd.getDriverVersion(); // 获得驱动程序的版本。返回字符串。 logger.info("数据库类型【" + dbType + "】数据库版本【" + dbVersion + "】数据库驱动名称【" + driverName + "】数据库驱动程序版本【" + driverVersion + "】"); } catch (SQLException e) { logger.error("获取DataBaseMetaData元数据出错", e); } return dbmd; }
Example 14
Source File: DatabaseStatusChecker.java From dubbox with Apache License 2.0 | 5 votes |
public Status check() { boolean ok; try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { ok = resultSet.next(); } finally { resultSet.close(); } if (message == null) { message = metaData.getURL() + " (" + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion() + ", " + getIsolation(metaData.getDefaultTransactionIsolation()) + ")"; } if (version == 0) { version = metaData.getDatabaseMajorVersion(); } } finally { connection.close(); } } catch (Throwable e) { logger.error(e.getMessage(), e); ok = false; } return new Status(! ok ? Status.Level.ERROR : (version < 5 ? Status.Level.WARN : Status.Level.OK), message); }
Example 15
Source File: MetaDataRegressionTest.java From r-course with MIT License | 4 votes |
/** * Tests fix for Bug# * * @throws Exception * if an error occurs */ public void testGetColumnsBug1099() throws Exception { try { this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099"); DatabaseMetaData dbmd = this.conn.getMetaData(); this.rs = dbmd.getTypeInfo(); StringBuilder types = new StringBuilder(); HashMap<String, String> alreadyDoneTypes = new HashMap<String, String>(); while (this.rs.next()) { String typeName = this.rs.getString("TYPE_NAME"); //String createParams = this.rs.getString("CREATE_PARAMS"); if ((typeName.indexOf("BINARY") == -1) && !typeName.equals("LONG VARCHAR")) { if (!alreadyDoneTypes.containsKey(typeName)) { alreadyDoneTypes.put(typeName, null); if (types.length() != 0) { types.append(", \n"); } int typeNameLength = typeName.length(); StringBuilder safeTypeName = new StringBuilder(typeNameLength); for (int i = 0; i < typeNameLength; i++) { char c = typeName.charAt(i); if (Character.isWhitespace(c)) { safeTypeName.append("_"); } else { safeTypeName.append(c); } } types.append(safeTypeName.toString()); types.append("Column "); types.append(typeName); if (typeName.indexOf("CHAR") != -1) { types.append(" (1)"); } else if (typeName.equalsIgnoreCase("enum") || typeName.equalsIgnoreCase("set")) { types.append("('a', 'b', 'c')"); } } } } this.stmt.executeUpdate("CREATE TABLE testGetColumnsBug1099(" + types.toString() + ")"); dbmd.getColumns(null, this.conn.getCatalog(), "testGetColumnsBug1099", "%"); } finally { this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099"); } }
Example 16
Source File: DataSourceStatusChecker.java From dubbox-hystrix with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public Status check() { ApplicationContext context = ServiceBean.getSpringContext(); if (context == null) { return new Status(Status.Level.UNKNOWN); } Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false); if (dataSources == null || dataSources.size() == 0) { return new Status(Status.Level.UNKNOWN); } Status.Level level = Status.Level.OK; StringBuilder buf = new StringBuilder(); for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) { DataSource dataSource = entry.getValue(); if (buf.length() > 0) { buf.append(", "); } buf.append(entry.getKey()); try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { if (! resultSet.next()) { level = Status.Level.ERROR; } } finally { resultSet.close(); } buf.append(metaData.getURL()); buf.append("("); buf.append(metaData.getDatabaseProductName()); buf.append("-"); buf.append(metaData.getDatabaseProductVersion()); buf.append(")"); } finally { connection.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); return new Status(level, e.getMessage()); } } return new Status(level, buf.toString()); }
Example 17
Source File: MetaDataRegressionTest.java From Komondor with GNU General Public License v3.0 | 4 votes |
/** * Tests fix for Bug# * * @throws Exception * if an error occurs */ public void testGetColumnsBug1099() throws Exception { try { this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099"); DatabaseMetaData dbmd = this.conn.getMetaData(); this.rs = dbmd.getTypeInfo(); StringBuilder types = new StringBuilder(); HashMap<String, String> alreadyDoneTypes = new HashMap<String, String>(); while (this.rs.next()) { String typeName = this.rs.getString("TYPE_NAME"); //String createParams = this.rs.getString("CREATE_PARAMS"); if ((typeName.indexOf("BINARY") == -1) && !typeName.equals("LONG VARCHAR")) { if (!alreadyDoneTypes.containsKey(typeName)) { alreadyDoneTypes.put(typeName, null); if (types.length() != 0) { types.append(", \n"); } int typeNameLength = typeName.length(); StringBuilder safeTypeName = new StringBuilder(typeNameLength); for (int i = 0; i < typeNameLength; i++) { char c = typeName.charAt(i); if (Character.isWhitespace(c)) { safeTypeName.append("_"); } else { safeTypeName.append(c); } } types.append(safeTypeName.toString()); types.append("Column "); types.append(typeName); if (typeName.indexOf("CHAR") != -1) { types.append(" (1)"); } else if (typeName.equalsIgnoreCase("enum") || typeName.equalsIgnoreCase("set")) { types.append("('a', 'b', 'c')"); } } } } this.stmt.executeUpdate("CREATE TABLE testGetColumnsBug1099(" + types.toString() + ")"); dbmd.getColumns(null, this.conn.getCatalog(), "testGetColumnsBug1099", "%"); } finally { this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099"); } }
Example 18
Source File: DataSourceStatusChecker.java From dubbox with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public Status check() { ApplicationContext context = ServiceBean.getSpringContext(); if (context == null) { return new Status(Status.Level.UNKNOWN); } Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false); if (dataSources == null || dataSources.size() == 0) { return new Status(Status.Level.UNKNOWN); } Status.Level level = Status.Level.OK; StringBuilder buf = new StringBuilder(); for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) { DataSource dataSource = entry.getValue(); if (buf.length() > 0) { buf.append(", "); } buf.append(entry.getKey()); try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { if (! resultSet.next()) { level = Status.Level.ERROR; } } finally { resultSet.close(); } buf.append(metaData.getURL()); buf.append("("); buf.append(metaData.getDatabaseProductName()); buf.append("-"); buf.append(metaData.getDatabaseProductVersion()); buf.append(")"); } finally { connection.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); return new Status(level, e.getMessage()); } } return new Status(level, buf.toString()); }
Example 19
Source File: DataSourceStatusChecker.java From dubbox with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public Status check() { ApplicationContext context = ServiceBean.getSpringContext(); if (context == null) { return new Status(Status.Level.UNKNOWN); } Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false); if (dataSources == null || dataSources.size() == 0) { return new Status(Status.Level.UNKNOWN); } Status.Level level = Status.Level.OK; StringBuilder buf = new StringBuilder(); for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) { DataSource dataSource = entry.getValue(); if (buf.length() > 0) { buf.append(", "); } buf.append(entry.getKey()); try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { if (! resultSet.next()) { level = Status.Level.ERROR; } } finally { resultSet.close(); } buf.append(metaData.getURL()); buf.append("("); buf.append(metaData.getDatabaseProductName()); buf.append("-"); buf.append(metaData.getDatabaseProductVersion()); buf.append(")"); } finally { connection.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); return new Status(level, e.getMessage()); } } return new Status(level, buf.toString()); }
Example 20
Source File: ConcurrentConnTest.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
public static void getTypeInfo(DatabaseMetaData dmd,PrintStream out) throws SQLException { ResultSet rs = dmd.getTypeInfo(); out.println("Submitted getTypeInfo request"); while (rs.next()) { // 1.TYPE_NAME String => Type name String typeName = rs.getString(1); // 2.DATA_TYPE short => SQL data type from java.sql.Types short dataType = rs.getShort(2); // 3.PRECISION int => maximum precision int precision = rs.getInt(3); // 4.LITERAL_PREFIX String => prefix used to quote a literal // (may be null) String literalPrefix = rs.getString(4); // 5.LITERAL_SUFFIX String => suffix used to quote a literal // (may be null) String literalSuffix = rs.getString(5); // 6.CREATE_PARAMS String => parameters used in creating the type // (may be null) String createParams = rs.getString(6); // 7.NULLABLE short => can you use NULL for this type? // typeNoNulls - does not allow NULL values // typeNullable - allows NULL values // typeNullableUnknown - nullability unknown short nullable = rs.getShort(7); // 8.CASE_SENSITIVE boolean=> is it case sensitive? boolean caseSensitive = rs.getBoolean(8); // 9.SEARCHABLE short => can you use "WHERE" based on this type: // typePredNone - No support // typePredChar - Only supported with WHERE .. LIKE // typePredBasic - Supported except for WHERE .. LIKE // typeSearchable - Supported for all WHERE .. short searchable = rs.getShort(9); // 10.UNSIGNED_ATTRIBUTE boolean => is it unsigned? boolean unsignedAttribute = rs.getBoolean(10); // 11.FIXED_PREC_SCALE boolean => can it be a money value? boolean fixedPrecScale = rs.getBoolean(11); // 12.AUTO_INCREMENT boolean => can it be used for an // auto-increment value? boolean autoIncrement = rs.getBoolean(12); // 13.LOCAL_TYPE_NAME String => localized version of type name // (may be null) String localTypeName = rs.getString(13); // 14.MINIMUM_SCALE short => minimum scale supported short minimumScale = rs.getShort(14); // 15.MAXIMUM_SCALE short => maximum scale supported short maximumScale = rs.getShort(15); // 16.SQL_DATA_TYPE int => unused // 17.SQL_DATETIME_SUB int => unused // 18.NUM_PREC_RADIX int => usually 2 or 10 //out.println(typeName); } rs.close(); }