JDBC database to obtain a variety of information

When a time through a JDBC connection data, we can obtain this information via the JDBC various data, such as the name of the database, model, table structure (and field), views, functions, procedures, access permissions, and so a series of information. Of course, if your intent, you can also access the data (as long as you have permission) all tables.

I had contact Sybase database, Sybase is rubbish, out waited a long time, even a table can not see, but no way to access the data talk, desperation after connected to the database via JDBC, one by one the various information data output.

After a lapse of two years, and recently doing a project, I found many people use the IDE to generate POJO from the table, each tool generates the rules are different, one SQL mapping inconsistent policies between Java types. But even a comment generate POJO properties are not, a lot of English description (in fact all fees fart, I only want the meaning of a field). It is very low, in general are starting from the table to the POJO from object modeling, rapid decomposition of the problem, establishing its domain model (which contains the link between the entity classes), if necessary, before the transition to the database modeling. Of course, these are not I can control.

Now we have to do this step to work, in order to maintain the consistency of the mappings can be defined down to a Java SQL Type the mapping between the Java Type, and then read the name of each table, traversing the name of its fields, type, comments Notes and other information, and the table, the field being used as an information POJO annotated member variable, which achieve their goals, as getter / setter methods, each tool generates well, you do not have to waste time doing what bored work.

Below MySQL5 example, to see the implementation process:

First, in order to describe the relationship between tables and columns, you must be a simple model:
table objects, listed objects-many relationship between.
{class TableInfoBean public
Private String tableName;
Private String tableComment;
Private List <ColumnBean> = new new columnList the ArrayList <ColumnBean> ();

public class ColumnBean {
private String columnName;
private String columnComment;
private String SqlType;

This relationship is very simple, so get used to save a table information.

Here's how to read the table information from the database.
Here a class dependent DatabaseMetaData, this object can be obtained from a database connection. With it everything is all right, and would like to know what to ask it:
DatabaseMetaData DatabaseMetaData = conn.getMetaData ();

    //获取所有表
    ResultSet tableSet = databaseMetaData.getTables(null, "%", "%", new String[]{"TABLE"});
    //获取tableName表列信息
    ResultSet columnSet = databaseMetaData.getColumns(null, "%", tableName, "%");

The code above will get two result sets DatabaseMetaData document control, we can through the column name of the result set to get the desired information, for example,

String tableName = tableSet.getString("TABLE_NAME");
String tableComment = tableSet.getString("REMARKS");

String columnName = columnSet.getString("COLUMN_NAME");
String columnComment = columnSet.getString("REMARKS");
String sqlType = columnSet.getString("DATA_TYPE");

Meaning I do not do very clearly explained.

Traverse the table one by one, then get a TableInfoBean collection, this collection is all the information in the database table, and with this information, generates xml, POJO simply pediatrics thing.

Guess you like

Origin blog.51cto.com/14512197/2443702