JDBC获取数据表字段名、注释等信息

来源:互联网 发布:gephi数据分析案例 编辑:程序博客网 时间:2024/06/11 13:16

http://blog.csdn.net/u011637069/article/details/52046662


需求:给定数据库信息和表名,扫描表的字段名、字段类型和注释。

注:数据库可以是Oracle、Mysql、DB2、SqlServer等。


解决方法:利用JDBC的DatabaseMetaData来获取数据库的元信息。

用法如下:

[java] view plain copy
  1. package util;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DatabaseMetaData;  
  5. import java.sql.DriverManager;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.util.ArrayList;  
  9. import java.util.HashMap;  
  10. import java.util.List;  
  11. import java.util.Map;  
  12. import java.util.Properties;  
  13.   
  14. /** 
  15.  *  
  16.  * <p>Description: 获取数据库基本信息的工具类</p> 
  17.  *  
  18.  * @author qxl 
  19.  * @date 2016年7月22日 下午1:00:34 
  20.  */  
  21. public class DbInfoUtil {  
  22.       
  23.     /** 
  24.      * 根据数据库的连接参数,获取指定表的基本信息:字段名、字段类型、字段注释 
  25.      * @param driver 数据库连接驱动 
  26.      * @param url 数据库连接url 
  27.      * @param user  数据库登陆用户名 
  28.      * @param pwd 数据库登陆密码 
  29.      * @param table 表名 
  30.      * @return Map集合 
  31.      */  
  32.     public static List getTableInfo(String driver,String url,String user,String pwd,String table){  
  33.         List result = new ArrayList();  
  34.           
  35.         Connection conn = null;       
  36.         DatabaseMetaData dbmd = null;  
  37.           
  38.         try {  
  39.             conn = getConnections(driver,url,user,pwd);  
  40.               
  41.             dbmd = conn.getMetaData();  
  42.             ResultSet resultSet = dbmd.getTables(null"%", table, new String[] { "TABLE" });  
  43.               
  44.             while (resultSet.next()) {  
  45.                 String tableName=resultSet.getString("TABLE_NAME");  
  46.                 System.out.println(tableName);  
  47.                   
  48.                 if(tableName.equals(table)){  
  49.                     ResultSet rs = conn.getMetaData().getColumns(null, getSchema(conn),tableName.toUpperCase(), "%");  
  50.   
  51.                     while(rs.next()){  
  52.                         //System.out.println("字段名:"+rs.getString("COLUMN_NAME")+"--字段注释:"+rs.getString("REMARKS")+"--字段数据类型:"+rs.getString("TYPE_NAME"));  
  53.                         Map map = new HashMap();  
  54.                         String colName = rs.getString("COLUMN_NAME");  
  55.                         map.put("code", colName);  
  56.                           
  57.                         String remarks = rs.getString("REMARKS");  
  58.                         if(remarks == null || remarks.equals("")){  
  59.                             remarks = colName;  
  60.                         }  
  61.                         map.put("name",remarks);  
  62.                           
  63.                         String dbType = rs.getString("TYPE_NAME");  
  64.                         map.put("dbType",dbType);  
  65.                           
  66.                         map.put("valueType", changeDbType(dbType));  
  67.                         result.add(map);  
  68.                     }  
  69.                 }  
  70.             }  
  71.         } catch (SQLException e) {  
  72.             e.printStackTrace();  
  73.         } catch (Exception e) {  
  74.             e.printStackTrace();  
  75.         }finally{  
  76.             try {  
  77.                 conn.close();  
  78.             } catch (SQLException e) {  
  79.                 e.printStackTrace();  
  80.             }  
  81.         }  
  82.           
  83.         return result;  
  84.     }  
  85.       
  86.     private static String changeDbType(String dbType) {  
  87.         dbType = dbType.toUpperCase();  
  88.         switch(dbType){  
  89.             case "VARCHAR":  
  90.             case "VARCHAR2":  
  91.             case "CHAR":  
  92.                 return "1";  
  93.             case "NUMBER":  
  94.             case "DECIMAL":  
  95.                 return "4";  
  96.             case "INT":  
  97.             case "SMALLINT":  
  98.             case "INTEGER":  
  99.                 return "2";  
  100.             case "BIGINT":  
  101.                 return "6";  
  102.             case "DATETIME":  
  103.             case "TIMESTAMP":  
  104.             case "DATE":  
  105.                 return "7";  
  106.             default:  
  107.                 return "1";  
  108.         }  
  109.     }  
  110.   
  111.     //获取连接  
  112.     private static Connection getConnections(String driver,String url,String user,String pwd) throws Exception {  
  113.         Connection conn = null;  
  114.         try {  
  115.             Properties props = new Properties();  
  116.             props.put("remarksReporting""true");  
  117.             props.put("user", user);  
  118.             props.put("password", pwd);  
  119.             Class.forName(driver);  
  120.             conn = DriverManager.getConnection(url, props);  
  121.         } catch (Exception e) {  
  122.             e.printStackTrace();  
  123.             throw e;  
  124.         }  
  125.         return conn;  
  126.     }  
  127.       
  128.     //其他数据库不需要这个方法 oracle和db2需要  
  129.     private static String getSchema(Connection conn) throws Exception {  
  130.         String schema;  
  131.         schema = conn.getMetaData().getUserName();  
  132.         if ((schema == null) || (schema.length() == 0)) {  
  133.             throw new Exception("ORACLE数据库模式不允许为空");  
  134.         }  
  135.         return schema.toUpperCase().toString();  
  136.   
  137.     }  
  138.   
  139.     public static void main(String[] args) {  
  140.           
  141.         //这里是Oracle连接方法  
  142.           
  143.         String driver = "oracle.jdbc.driver.OracleDriver";  
  144.         String url = "jdbc:oracle:thin:@192.168.12.44:1521:orcl";  
  145.         String user = "bdc";  
  146.         String pwd = "bdc123";  
  147.         //String table = "FZ_USER_T";  
  148.         String table = "FZ_USER_T";  
  149.           
  150.         //mysql  
  151.         /* 
  152.         String driver = "com.mysql.jdbc.Driver"; 
  153.         String user = "root"; 
  154.         String pwd = "123456"; 
  155.         String url = "jdbc:mysql://localhost/onlinexam" 
  156.                 + "?useUnicode=true&characterEncoding=UTF-8"; 
  157.         String table = "oe_student"; 
  158.         */  
  159.           
  160.         List list = getTableInfo(driver,url,user,pwd,table);  
  161.         System.out.println(list);  
  162.     }  
  163.       
  164. }  
注:需要导入数据库连接驱动

此工具类无需修改,适用于Oracle、Mysql、DB2、SqlServer数据库。

原创粉丝点击