How Java obtains the fields and field types of a database

Source: Internet
Author: User

Java Gets the field names of the fields in the table of the database, with the following code:

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++) {
Get the number of all columns and the actual number of columns
int columnCount = Data.getcolumncount ();
Gets the column name of the specified column
String columnName = Data.getcolumnname (i);
Gets the column value for the specified column
int columntype = Data.getcolumntype (i);
Gets the data type name of the specified column
String columntypename = Data.getcolumntypename (i);
The name of the catalog where
String catalogname = Data.getcatalogname (i);
Classes that correspond to data types
String columnclassname = Data.getcolumnclassname (i);
Maximum number of characters in a database type
int columndisplaysize = Data.getcolumndisplaysize (i);
The title of the default column
String ColumnLabel = Data.getcolumnlabel (i);
Get the schema of the column
String schemaName = Data.getschemaname (i);
Accuracy of a column type (length of type)
int precision = data.getprecision (i);
Number of digits after the decimal point
int scale = Data.getscale (i);
Get the table name for a column
String tableName = Data.gettablename (i);
Whether to increment automatically
Boolean isautoinctement = Data.isautoincrement (i);
Whether it is a currency type in the database
Boolean iscurrency = Data.iscurrency (i);
is empty
int isnullable = data.isnullable (i);
is read-only
Boolean isreadonly = Data.isreadonly (i);
Can appear in the 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 + "type, return the number in the SqlType:" + ColumnType);
System.out.println ("Get column" + i + "Data type name:" + columntypename);
System.out.println ("Get column" + i + "Location of the Catalog name:" + catalogname);
System.out.println ("Get column" + i + "corresponding data type class:" + Columnclassname);
System.out.println ("Get column" + i + "maximum number of characters in the database type:" + columndisplaysize);
System.out.println ("Get column" + i + "The title of the default column:" + ColumnLabel);
System.out.println ("Get column" + i + "mode:" + schemaName);
System.out.println ("Get column" + i + "type of accuracy (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 ();
}
}
}


In practice, you can also use case clauses to handle different types of fields:

Switch (ColumnType) {
Case Types.NUMERIC:classFactory.setprop (Entity, ColumnName, Rs.getlong (i));
Break
Case Types.VARCHAR:classFactory.setprop (Entity, ColumnName, rs.getstring (i));
Break
Case Types.DATE:classFactory.setprop (Entity, ColumnName, rs.getdate (i));
Break
Case Types.TIMESTAMP:classFactory.setprop (Entity, ColumnName, Rs.gettimestamp (i));
Break
Case Types.TIME:classFactory.setprop (Entity, ColumnName, Rs.gettime (i));
Break
Case Types.BOOLEAN:classFactory.setprop (Entity, ColumnName, Rs.getboolean (i));
Break
Case Types.ARRAY:classFactory.setprop (Entity, ColumnName, Rs.getarray (i));
Break
Case Types.BIGINT:classFactory.setprop (Entity, ColumnName, Rs.getint (i));
Break
Case Types.BINARY:classFactory.setprop (Entity, ColumnName, Rs.getbinarystream (i));
Break
Case Types.BLOB:classFactory.setprop (Entity, ColumnName, Rs.getblob (i));
Break
Case Types.CHAR:classFactory.setprop (Entity, ColumnName, rs.getstring (i));
Break
Case Types.INTEGER:classFactory.setprop (Entity, ColumnName, Rs.getint (i));
Break
Case Types.DOUBLE:classFactory.setprop (Entity, columnname,rs.getdouble (i));
Break
Case Types.FLOAT:classFactory.setprop (Entity, ColumnName, rs.getfloat (i));
Break
Case Types.SMALLINT:classFactory.setprop (Entity, ColumnName, Rs.getint (i));
Break
Case Types.DECIMAL:classFactory.setprop (Entity, ColumnName, Rs.getlong (i));
Break
Default:classFactory.setprop (Entity, ColumnName, Rs.getobject (i));
Break
}

How Java obtains the fields and field types of a database

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.