Oracle reads the table structure in the library

Source: Internet
Author: User

(Study record)

In the Code table class and field class please refer to: http://meijia.blog.51cto.com/8684191/1563874

The relevant parameters can be adjusted by reference to the API.

(Same note format)


1. The method is as follows

Public list<table> Export () {

list<table> tablelist = new arraylist<table> ();

Connection conn = Dbutil.getconnection ();

ResultSet tablers = null; Store metadata

ResultSet Colrs = null;//Storage Table Metadata

try {


DatabaseMetaData Dbmd = Conn.getmetadata ();//Returns the metadata of the database to which this Connection object is connected


Get all Tables

list<string> tablenamelist = new arraylist<string> ();

Tablers = Dbmd.gettables (null, "%", "%", new string[]{"TABLE"}); All tables

while (Tablers.next ()) {

String tableName = tablers.getstring ("table_name");//Table name

Tablenamelist.add (TableName);

}


List<field> FieldList = null;//stores all fields for each table

Table table = null;

for (String name:tablenamelist) {

Table = new Table ();

Get the fields for a table

Colrs = Dbmd.getcolumns (null, "%", name, "%");//field of the current table

Field field = NULL;

FieldList = new arraylist<field> ();

while (Colrs.next ()) {

field = new field ();

String columnName = colrs.getstring ("column_name");//Name

String ColumnType = colrs.getstring ("type_name");//Type

int datasize = Colrs.getint ("column_size");//Field length

int digits = Colrs.getint ("Decimal_digits");

int nullable = Colrs.getint ("nullable");//return 1 means that it can be null, and 0 means not NULL

Field.setcolumnname (ColumnName);

Field.settypename (ColumnType);

Field.setcolumnsize (datasize);

Field.setdecimal_digits (digits);

Field.setnullable (nullable);


Fieldlist.add (field);

}

Table.settablename (name);

Table.setfield (FieldList);

Tablelist.add (table);

}

} catch (SQLException ex) {

Logger.getlogger (ExportOracleTable.class.getName ()). log (Level.severe, NULL, ex);

} finally {

if (Colrs! = null) {

try {

Colrs.close ();

} catch (SQLException ex) {

Logger.getlogger (ExportOracleTable.class.getName ()). log (Level.severe, NULL, ex);

}

}

if (tablers! = null) {

try {

Tablers.close ();

} catch (SQLException ex) {

Logger.getlogger (ExportOracleTable.class.getName ()). log (Level.severe, NULL, ex);

}

}

IF (conn! = null) {

try {

Conn.close ();

} catch (SQLException ex) {

Logger.getlogger (ExportOracleTable.class.getName ()). log (Level.severe, NULL, ex);

}

}

}

return tablelist;

}


This article is from the "I Will work" blog, please be sure to keep this source http://meijia.blog.51cto.com/8684191/1563879

Oracle reads the table structure in the library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.