TestDataTypespublic class TestDataTypes extends Object
Fields Summary |
---|
Connection | conn |
Constructors Summary |
---|
public TestDataTypes()
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(
"jdbc:oracle:oci8:@dssw2k01", "scott", "tiger");
// "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
}
catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
|
Methods Summary |
---|
protected void | finalize()
if (conn != null) { try { conn.close(); } catch (SQLException ignore) { } }
super.finalize();
| private java.lang.String | formatDataType(int type)
switch (type) {
case Types.ARRAY:
return "ARRAY";
case Types.BIGINT:
return "BIGINT";
case Types.BINARY:
return "BINARY";
case Types.BIT:
return "BIT";
case Types.BLOB:
return "BLOB";
case Types.CHAR:
return "CHAR";
case Types.CLOB:
return "CLOB";
case Types.DATE:
return "DATE";
case Types.DECIMAL:
return "DECIMAL";
case Types.DISTINCT:
return "DISTINCT";
case Types.DOUBLE:
return "DOUBLE";
case Types.FLOAT:
return "FLOAT";
case Types.INTEGER:
return "INTEGER";
case Types.JAVA_OBJECT:
return "JAVA_OBJECT";
case Types.LONGVARBINARY:
return "LONGVARBINARY";
case Types.LONGVARCHAR:
return "LONGVARCHAR";
case Types.NULL:
return "NULL";
case Types.NUMERIC:
return "NUMERIC";
case Types.OTHER:
return "OTHER";
case Types.REAL:
return "REAL";
case Types.REF:
return "REF";
case Types.SMALLINT:
return "SMALLINT";
case Types.STRUCT:
return "STRUCT";
case Types.TIME:
return "TIME";
case Types.TIMESTAMP:
return "TIMESTAMP";
case Types.TINYINT:
return "TINYINT";
case Types.VARBINARY:
return "VARBINARY";
case Types.VARCHAR:
return "VARCHAR";
default:
return "UNKNOWN";
}
| private java.lang.String | formatNullable(int nullable)
switch (nullable) {
case ResultSetMetaData.columnNoNulls:
return "false";
case ResultSetMetaData.columnNullable:
return "true";
default:
return "";
}
| public static void | main(java.lang.String[] args)
new TestDataTypes().process();
| public void | process()
int rows = 0;
ResultSet rslt = null;
ResultSetMetaData meta = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
rslt = stmt.executeQuery(
"select b.dummy, " +
" a.a_smallint smallint_alias, " +
" a_int int_alias, " +
" a_integer integer_alias, " +
" a_numeric numeric_alias, " +
" a_decimal decimal_alias, " +
" a_real real_alias, " +
" a_float float_alias, " +
" a_double double_alias, " +
" a_number number_alias, " +
" a_character character_alias, " +
" a_char char_alias, " +
" a_character_varying character_varying_alias, " +
" a_char_varying char_varying_alias, " +
" a_varchar2 varchar2_alias, " +
" a_date date_alias, " +
" a_long long_alias " +
"from scott.test_data_types a, sys.dual b ");
meta = rslt.getMetaData();
System.out.println("column count = " + meta.getColumnCount());
System.out.println("schema name = " + meta.getSchemaName(1));
System.out.println("table name = " + meta.getTableName(1));
System.out.println("catalog name = " + meta.getCatalogName(1));
System.out.println("");
for (int i=1;i <= meta.getColumnCount();i++) {
System.out.println("column name = " + meta.getColumnName(i));
System.out.println("column label = " + meta.getColumnLabel(i));
System.out.println("column class name = " + meta.getColumnClassName(i));
System.out.println("column type name = " + meta.getColumnTypeName(i));
System.out.println("column type = " + meta.getColumnType(i) +
" (java.sql.Types." + formatDataType(meta.getColumnType(i)) + ")");
System.out.println("column display size = " + meta.getColumnDisplaySize(i));
System.out.println("precision = " + meta.getPrecision(i));
System.out.println("scale = " + meta.getScale(i));
System.out.println("is auto increment = " + meta.isAutoIncrement(i));
System.out.println("is case sensitive = " + meta.isCaseSensitive(i));
System.out.println("is signed = " + meta.isSigned(i));
System.out.println("is currency = " + meta.isCurrency(i));
System.out.println("is nullable = " + formatNullable(meta.isNullable(i)));
System.out.println("is searchable = " + meta.isSearchable(i));
System.out.println("is read only = " + meta.isReadOnly(i));
System.out.println("is writeble = " + meta.isWritable(i));
System.out.println("is definitely writable = " + meta.isDefinitelyWritable(i));
System.out.println(" ");
}
while (rslt.next()) {
rows++;
}
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (rslt != null) try { rslt.close(); } catch (SQLException ignore) { }
if (stmt != null) try { stmt.close(); } catch (SQLException ignore) { }
}
|
|