Java views database information and corresponding table information
Public class DataBaseMessage {
Public static void main (String [] args) {
GetMessage ()
}
Public static void getMessage () {
Connection connection = null
ResultSet rs = null
Try {
Connection = get connection
DatabaseMetaData dbMetaData = connection.getMetaData ()
System.out.println ("Database related Information:")
System.out.println ("known users:" + dbMetaData.getUserName ())
System.out.println ("comma-separated list of system functions:" + dbMetaData.getSystemFunctions ())
System.out.println ("comma-separated list of time and date functions:" + dbMetaData.getTimeDateFunctions ())
System.out.println ("comma-separated list of string functions:" + dbMetaData.getStringFunctions ())
System.out.println ("supplier's preferred term for 'schema':" + dbMetaData.getSchemaTerm ())
System.out.println ("URL:" + dbMetaData.getURL ())
System.out.println ("allow read-only:" + dbMetaData.isReadOnly ())
System.out.println ("Product name:" + dbMetaData.getDatabaseProductName ())
System.out.println ("version:" + dbMetaData.getDatabaseProductVersion ())
System.out.println ("name of the driver:" + dbMetaData.getDriverName ())
System.out.println ("driver version:" + dbMetaData.getDriverVersion ())
/ / query all the information of the t_file table
Rs = dbMetaData.getColumns (null, null, "Table name", null)
/ / get the information of each record in the t_file table
While (rs.next ()) {
System.out.println (); System.out.println ()
String column_name = rs.getString ("COLUMN_NAME")
String type_name = rs.getString ("TYPE_NAME")
Int column_size = rs.getInt ("COLUMN_SIZE")
Boolean is_nullable = rs.getBoolean ("IS_NULLABLE")
String column_def = rs.getString ("COLUMN_DEF")
System.out.println ("column_name:" + column_name+ "* type_name:" + type_name+ "* column_size:" + column_size+ "* is_nullable:" + is_nullable + "* column_def:" + column_def)
}
} catch (Exception e) {
E.printStackTrace ()
} finally {
Try {
/ / connection.close ()
/ / rs.close ()
} catch (Exception e2) {
E2.printStackTrace ()
}
}
}
}