When connecting to SQL using Java, obtain the attributes of each column in the table

Source: Internet
Author: User
Tags sql using
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: // localhost: 3306/struts2demo ";
String user = "root ";
String pass = "123456 ";
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 users ";
Preparedstatement stmt;
Try {
Stmt = conn. preparestatement (SQL );
Resultset rs1_stmt.exe cutequery (SQL );
Resultsetmetadata DATA = Rs. getmetadata ();
While (Rs. Next ()){
For (INT I = 1; I <= data. getcolumncount (); I ++ ){
// Obtain the number of all columns and the actual number of Columns
Int columncount = data. getcolumncount ();
// Obtain the name of the specified Column
String columnname = data. getcolumnname (I );
// Obtain the column value of the specified Column
String columnvalue = Rs. getstring (I );
// Obtain the data type of the specified Column
Int columntype = data. getcolumntype (I );
// Obtain the data type name of the specified Column
String columntypename = data. getcolumntypename (I );
// The Catalog name
String catalogname = data. getcatalogname (I );
// Corresponding data type class
String columnclassname = data. getcolumnclassname (I );
// Maximum number of characters in the database
Int columndisplaysize = data. getcolumndisplaysize (I );
// Default Column Title
String columnlabel = data. getcolumnlabel (I );
// Obtain the column Mode
String schemaname = data. getschemaname (I );
// Precision of a column type (type length)
Int precision = data. getprecision (I );
// Number of digits after the decimal point
Int scale = data. getscale (I );
// Obtain the table name corresponding to a column
String tablename = data. gettablename (I );
// Auto Increment
Boolean isautoinctement = data. isautoincrement (I );
// Whether it is a currency type in the database
Boolean iscurrency = data. iscurrency (I );
// Whether it is null
Int isnullable = data. isnullable (I );
// Whether it is read-only
Boolean isreadonly = data. isreadonly (I );
// Whether it 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 column" + I + "field value:" + columnvalue );
System. Out. println ("retrieve column" + I + "type, return number in sqltype:" + columntype );
System. Out. println ("Get column" + I + "data type name:" + columntypename );
System. Out. println ("retrieve column" + I + "catalog name:" + catalogname );
System. Out. println ("Get column" + I + ":" + 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 precision (type length):" + precision );
System. Out. println ("Get column" + I + "digits after decimal point:" + scale );
System. Out. println ("Get column" + I + "corresponding table name:" + tablename );
System. Out. println ("Get column" + I + "auto increment:" + isautoinctement );
System. Out. println ("retrieve columns" + I + "in the database for currency type:" + iscurrency );
System. Out. println ("Get column" + I + "blank:" + isnullable );
System. Out. println ("Get column" + I + "read-only:" + isreadonly );
System. Out. println ("retrieve column" + I + "can appear in where:" + issearchable );
}
}
} Catch (sqlexception e ){
System. Out. println ("database connection failed ");
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.