Recently need to get information about the field in the database, in this collation, ready for later use (I feel can put these commonly used methods, etc., packaged up, later used on the more convenient)
Java Gets the field name of each field in the table in the database, and the code is as follows:
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.preparedstatement;
import java.sql.resultsetmetadata;
import java.sql.sqlexception; public class testdemo {public static connection getconnection () {Connection
conn = null;
try {class.forname ("Com.mysql.jdbc.Driver");
string url = "jdbc:mysql://Database IP Address: 3306/database name";
string user = "Database user name";
string pass = "Database user password";
Conn = drivermanager.getconnection (Url, user, pass); } catch (classnotfoundexception e) {e.printstacktrace ();}
catch (sqlexception e) {e.printstacktrace ();} return conn; } public static void main (String[] args) {connection conn =
Getconnection ();
string sql = "Select * from accesstype"; preparedstatement stmt;
try {stmt = conn.preparestatement (SQL);
Resultset rs = stmt.executequery (SQL);
Resultsetmetadata data = rs.getmetadata (); for (Int i = 1; i <= data.getcolumncount (); i++) {//
To obtain the number of all columns and the actual number of int columncount = data.getcolumncount ();
Get the column name of the specified column string columnname = data.getcolumnname (i);
Get the column value of the specified column int columntype = data.getcolumntype (i);
Get the data type name of the specified column string columntypename = data.getcolumntypename (i);
Catalog name String catalogname = data.getcatalogname (i);
Class String columnclassname = data.getcolumnclassname (i) of the corresponding data type;
Maximum number of characters in the database type int columndisplaysize = data.getcolumndisplaysize (i);
The default column headings String columnlabel = data.getcolumnlabel (i);
Get the mode of the column string schemaname = data.getschemaname (i); The precision of a column type (the length of the type) int precision = data.getprecision (i);
Number of digits after decimal point int scale = data.getscale (i);
get a column corresponding to the table name String tablename = data.gettablename (i);
Whether automatic increment boolean isautoinctement = data.isautoincrement (i);
Whether the currency type boolean iscurrency = data.iscurrency in the database (i);
Whether it is empty int isnullable = data.isnullable (i);
is read-only boolean isreadonly = data.isreadonly (i);
can appear in where boolean issearchable = data.issearchable (i);
System.out.println (ColumnCount);
System.out.println ("Get Column + i + " field name: " + columnname);"
System.out.println ("Get the type of column" + i + ", return the number in SqlType:" + columntype);
System.out.println ("Get column" + i + "Data type name:" + columntypename);
System.out.println ("Get column" + i + "Catalog name:" + catalogname); System.out.println ("Get column" + i + "classes of corresponding data types:" + columnclassname);
System.out.println ("Get column" + i + "maximum number of characters in the database type:" + columndisplaysize);
System.out.println ("Get column" + i + "default column title:" + columnlabel);
System.out.println ("Get column" + i + "mode:" + schemaname);
System.out.println ("Get Column" + i + "type of precision (length of type):" + precision);
System.out.println ("Get column" + i + "number of digits after decimal point:" + scale);
System.out.println ("Get column" + i + "corresponding table name:" + tablename);
System.out.println ("Get column" + i + "is automatically incremented:" + isautoinctement);
System.out.println ("Get column" + i + "in the database is currency type:" + iscurrency);
System.out.println ("Get column" + i + "is empty:" + isnullable);
System.out.println ("Get column" + i + "is read-only:" + isreadonly); SYSTEM.OUT.PRINTLN (Get column + i + can appear in Where: "+ issearchable);
}} catch (Sqlexception e) {e.printstacktrace ();} }