jdbc gets table structure, primary key

From the blog: http://blog.csdn.net/ocean1010/article/details/7266042

Suppose there is a con 
DatabaseMetaData dbmd = con.getMetaData(); 
rs = dbmd.getColumns(con.getCatalog(), schema, tableName, null); 
rs.getString(DATA_TYPE) SQL type of java.sql.Types 
rs.getString( COLUMN_SIZE) column size. For types char or date, the size of the column is the maximum number of characters, and for types numeric and decimal, the size of the column is the precision. 
rs.getString(DECIMAL_DIGITS) the number of digits in the fractional part

In JDBC, the relevant information of a specific table is obtained through MetaData. You can query which tables are in the database, which fields are in the tables, the attributes of the fields, and so on. In MetaData, through a series of getXXX functions, the information is stored in the ResultSet, and then returned to the user. There are also a lot of explanations about MetaData on the Internet. Here I just record the simple use of JDBC and the method of obtaining data table related information from the perspective of my own learning. 
First of all, http://hometown.aol.com/kgb1001001/Articles/JDBCMetadata/JDBC_Metadata.htm  There is an article called "Understanding JDBC MetaData", which explains it well.

The following is the code for obtaining table information under my JDBC. I am using MySQL 5.0 as a test platform.

1. The code for JDBC to connect to MYSQL is very standard and simple. 
class.forName("com.mysql.jdbc.Driver").newInstance(); 
Connection conn = DriverManager 
.getConnection("jdbc:mysql://localhost/test?user=root&password=123456");

2. The following is the information to obtain the table. 
m_DBMetaData = m_Connection.getMetaData(); 
ResultSet tableRet = m_DBMetaData.getTables(null, "%",m_TableName,new String[]{"TABLE"}); 
where "%" means *, which means any and all . Where m_TableName is the name of the data table to be obtained, if you want to obtain the names of all tables, you can use "%" as a parameter.

3. Extract the name of the table. 
while(tableRet.next) System.out.println(tableRet.getString("TABLE_NAME"));

Through getString("TABLE_NAME"), you can get the name of the table. 
It can be seen from this that, through the return of the interface of getTables, JDBC stores all its results in a table-like memory structure, and the field with the name TABLE_NAME is the name of each table.

4. 提取表内的字段的名字和类型 
String columnName; 
String columnType; 
ResultSet colRet = m_DBMetaData.getColumns(null,"%", m_TableName,"%"); 
while(colRet.next()) { 
columnName = colRet.getString("COLUMN_NAME"); 
columnType = colRet.getString("TYPE_NAME"); 
int datasize = colRet.getInt("COLUMN_SIZE"); 
int digits = colRet.getInt("DECIMAL_DIGITS"); 
int nullable = colRet.getInt("NULLABLE"); 
System.out.println(columnName+" "+columnType+" "+datasize+" "+digits+" "+ 
nullable); 
}

In JDBC, the query on the field is realized through the interface of getColumns. Like getTables, "%" means all arbitrary (fields), and m_TableName is the name of the data table.

getColumns的返回也是将所有的字段放到一个类似的内存中的表,而COLUMN_NAME就是字段的名字,TYPE_NAME就是数据类型,比如"int","int unsigned"等等,COLUMN_SIZE返回整数,就是字段的长度,比如定义的int(8)的字段,返回就是8,最后NULLABLE,返回1就表示可以是Null,而0就表示Not Null。
-------------------
大多数数据库有许多主键,但是在一个表中不允许两条记录的同一个主键具有相同的值。可以使用Java Database Connectivity(JDBC)来判断一个数据表的主键。 
  JDBC具有强大的元数据处理能力。java.sql.Connection类和java.sql.ResultSet类可以通过调用其getMetaData方法进行反射,例如: 
  // 对java.sql中所有的类 
  Connection connection = ..... 
  DatabaseMetaData dbMeta = connection.getMetaData(); 
  ResultSet rset = ..... 
  ResultSetMetaData rsMeta = rset.getMetaData(); 
  java.sql.DatabaseMetaData类包含一个查找数据表主键的方法。你需要知道表的名字,catalog名和schema名。如果不知道catalog和schema,那么你可以不使用它们而输入“null”。例如: 
  // 查找一个名字为“Comment”的表的主键 
  // 没有catalog或schema,都设置为null 
  ResultSet pkRSet = dbMeta.getPrimaryKeys(nullnull, "Comment"); 
  while( pkRSet.next() ) { 
  System.err.println("****** Comment ******"); 
  System.err.println("TABLE_CAT : "+pkRSet.getObject(1)); 
  System.err.println("TABLE_SCHEM: "+pkRSet.getObject(2)); 
  System.err.println("TABLE_NAME : "+pkRSet.getObject(3)); 
  System.err.println("COLUMN_NAME: "+pkRSet.getObject(4)); 
  System.err.println("KEY_SEQ : "+pkRSet.getObject(5)); 
  System.err.println("PK_NAME : "+pkRSet.getObject(6)); 
  System.err.println("****** ******* ******"); 
  } 
  在这个例子中表“Comment”具有一个叫做“comment_id”的主键。 
  下面是上面这些代码在MySQL上的输出: 
  ****** Comment ****** 
  TABLE_CAT : 
  TABLE_SCHEM: 
  TABLE_NAME : Comment 
  COLUMN_NAME: column_id 
  KEY_SEQ : 1 
  PK_NAME : column_id 
  ****** ******* ****** 
  存在PK_NAME的原因是有时会为一个主键使用列名之外的名字。而KEY_SEQ表示了主键的顺序位置。有些使用字母顺序保存主键的数据库会为KEY_SEQ返回0。 
  当创建通用的数据库应用时,查找一个表的主键是很基本的。JDBC的MetaData类提供了所需的数据库反射机制,从而使得这些应用的实现成为可能。 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326472660&siteId=291194637