威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
数据库>> Mysql Sqlserver Oracle SQlite Access Sybase SQL其他
当前位置:首页 > 数据库 > Mysql
上一节 下一节
 基于Java获取Mysql表结构的步骤

基于java获取mysql表结构的方法

class.forname("com.mysql.jdbc.driver").newinstance();
	connection conn = drivermanager
				.getconnection("jdbc:mysql://localhost:3306/mall?user=root&password=123456");
	databasemetadata meta = (databasemetadata) conn.getmetadata();

	resultset  rs = meta.getcolumns(null, "%", "t_mall_returnorderinfo", "%");
		
	while (rs.next())  {  
            // table catalog (may be null)  
            string tablecat = rs.getstring("table_cat");  
            // table schema (may be null)  
            string tableschemaname = rs.getstring("table_schem");  
            // table name  
            string tablename_ = rs.getstring("table_name");  
            // column name  
            string columnname = rs.getstring("column_name");  
            
            // sql type from java.sql.types  
            int datatype = rs.getint("data_type");  
            
            // data source dependent type name, for a udt the type name is  
            // fully qualified  
            string datatypename = rs.getstring("type_name"); 
            system.out.println(columnname + "    " + datatypename);
            // table schema (may be null)  
            int columnsize = rs.getint("column_size");  
            // the number of fractional digits. null is returned for data  
            // types where decimal_digits is not applicable.  
            int decimaldigits = rs.getint("decimal_digits");  
            // radix (typically either 10 or 2)  
            int numprecradix = rs.getint("num_prec_radix");  
            // is null allowed.  
            int nullable = rs.getint("nullable");  
            // comment describing column (may be null)  
            string remarks = rs.getstring("remarks");  
            // default value for the column, which should be interpreted as  
            // a string when the value is enclosed in single quotes (may be  
            // null)  
            string columndef = rs.getstring("column_def");  
            //                
            int sqldatatype = rs.getint("sql_data_type");  
            //                
            int sqldatetimesub = rs.getint("sql_datetime_sub");  
            // for char types the maximum number of bytes in the column  
            int charoctetlength = rs.getint("char_octet_length");  
            // index of column in table (starting at 1)  
            int ordinalposition = rs.getint("ordinal_position");  
            // iso rules are used to determine the nullability for a column.  
            // yes --- if the parameter can include nulls;  
            // no --- if the parameter cannot include nulls  
            // empty string --- if the nullability for the parameter is  
            // unknown  
            string isnullable = rs.getstring("is_nullable");  
            // indicates whether this column is auto incremented  
            // yes --- if the column is auto incremented  
            // no --- if the column is not auto incremented  
            // empty string --- if it cannot be determined whether the  
            // column is auto incremented parameter is unknown  
            string isautoincrement = rs.getstring("is_autoincrement");  
            system.out.println(tablecat + "-" + tableschemaname + "-" + tablename_ + "-" + columnname + "-"  
                    + datatype + "-" + datatypename + "-" + columnsize + "-" + decimaldigits + "-" + numprecradix  
                    + "-" + nullable + "-" + remarks + "-" + columndef + "-" + sqldatatype + "-" + sqldatetimesub  
                    + charoctetlength + "-" + ordinalposition + "-" + isnullable + "-" + isautoincrement + "-");
        }  
	conn.close();

?


申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!
上一节 下一节
相关教程  
其他教程  
Mysql
Sqlserver
Oracle
SQlite
Access
Sybase
SQL其他

违法和不良信息举报中心】邮箱:254677821@qq.com
Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821