metadata (MetaData):

  metadata (MetaData), The data that defines the data . such as , We're going to search for a song ( Songs themselves are data ), You can use the title of the song , singer , Album and other information to search , The names of these songs , singer , The album is the metadata of this song . Therefore, the metadata of the database is the data that indicates the database information .

JDBC There are three main interfaces to handle the database , Namely :

   Connection : from Connection Object's getMetaData() Method gets the DatabaseMetaData object .

   PreparedStatement : from PreparedStatement Object's getParameterMetaData () Method gets the
ParameterMetaData object .

   ResultSet : from ResultSet Object's getMetaData() Method gets the ResultSetMetaData object .

One ,DatabaseMetaData( database metadata )

 DatabaseMetaData By Connection Object through getMetaData Method , It mainly encapsulates some comprehensive information of the database itself , For example, the product name of the database , The version number of the database , Database URL, Whether transactions are supported, etc , More information can be obtained , Specific can refer to DatabaseMetaData Of API file .
  Here's a little bit about DatabaseMetaData Common methods of :

   getDatabaseProductName: Get the product name of the database

   getDatabaseProductVersion: Gets the version number of the database

   getUserName: Gets the user name of the database

   getURL: Gets the database connection's URL

   getDriverName: Gets the driver name of the database

   driverVersion: Get the driver version number of the database

   isReadOnly: Check whether the database only allows read operations

   supportsTransactions: Check whether the database supports transactions

Code demonstration :
@Test public void dataBaseMetaDataTest() throws Exception { // obtain Connection object
Connection con = JdbcUtil.getConnection(); // obtain DataBaseMetaData object
DatabaseMetaData metaData = con.getMetaData(); System.out.println(" Get the product name of the database : "
+ metaData.getDatabaseProductName()); System.out.println(" Gets the version number of the database : " +
metaData.getDatabaseProductVersion()); System.out.println(" Gets the user name of the database : " +
metaData.getUserName()); System.out.println(" Get database's URL: " + metaData.getURL());
System.out.println(" Gets the driver name of the database : " + metaData.getDriverName()); System.out
.println(" Get the driver version number of the database : " + metaData.getDriverVersion()); System.out.println(
" Check whether the database only allows read operations : " + metaData.isReadOnly()); System.out.println(" Check whether the database supports transactions : "
+ metaData.supportsTransactions()); }
Operation results :
Get the product name of the database : MySQL Gets the version number of the database : 5.7.22-log Gets the user name of the database : root@localhost Get database's URL:
jdbc:mysql://localhost:3306/day16?characterEncoding=utf-8 Gets the driver name of the database : MySQL-AB
JDBC Driver Get the driver version number of the database : mysql-connector-java-5.1.7 ( Revision: ${svn.Revision}
) Check whether the database only allows read operations : false Check whether the database supports transactions : true
Two ,ParameterMetaData( Parameter metadata )

 ParameterMetaData By PreparedStatement Object through getParameterMetaData Method , It is mainly aimed at PreparedStatement Object and its precompiled SQL Command statements provide some information , Like, for example ”insert
into account(id,name,money)
values(???)” Such precompiling SQL sentence ,ParameterMetaData Can provide the number of placeholder parameters , For the specified location holder SQL Type and so on .
  Here's a little bit about ParameterMetaData Common methods of :

    getParameterCount: Get precompiled SQL The number of placeholder parameters in the statement

Code demonstration :
@Test public void parameterMetaDataTest() throws Exception{ // obtain Connection object
Connection con = JdbcUtil.getConnection();// establish SQL String sql = "select * from
dept where id=? and sname=?"; // precompile SQL, obtain prepareStatement object PreparedStatement
prepareStatement = con.prepareStatement(sql);// obtain parameterMetaData object
ParameterMetaData parameterMetaData = prepareStatement.getParameterMetaData();
// Get the number of parameters int count = parameterMetaData.getParameterCount(); // output
System.out.println(" The number of placeholders is : " + count); }
Output results :

The number of placeholders is : 2

Three ,ResultSetMetaData( ResultSet Metadata )

 ResultSetMetaData By ResultSet Object through getMetaData Method , It is mainly for database execution SQL The result set object obtained by the script command ResultSet Some of the information provided in , For example, the number of columns in the result set , Specifies the name of the column , Of the specified column SQL Type and so on , It can be said that this is a very important object for the framework .
  Here's a little bit about ResultSetMetaData Common methods of :

    getColumnCount: Gets the number of column items in the result set

    getColumnType: For the specified column SQL Type corresponds to Java in Types Class

    getColumnTypeName: For the specified column SQL type

    getClassName: Gets the specified column SQL Type corresponds to Java Type in ( Package name plus class name )

Code demonstration :

Correspondence table :

@Test public void resultSetMetaDataTest(){ Connection con = null;
PreparedStatement stmt =null; ResultSet rs = null; try { // obtain Connection object con =
JdbcUtil.getConnection(); String sql ="select * from student";
// precompile sql sentence , obtain PreparedStatement object stmt = con.prepareStatement(sql); // implement sql rs =
stmt.executeQuery();// Get the result set meta object ResultSetMetaData metaData = rs.getMetaData();
System.out.println(" Gets the number of columns in the result set : " + metaData.getColumnCount()); System.out
.println(" Gets the name of the specified column : " + metaData.getColumnName(1)); System.out.println(
" For the specified column SQL Type corresponds to java.sql.Types Class : " + metaData.getColumnType(2)); System.out
.println(" For the specified column SQL type : " + metaData.getColumnTypeName(1)); System.out.println(
" Gets the specified column SQL Type corresponds to Java Type of : " + metaData.getColumnClassName(1)); System.out.println(
" Gets the name of the table where the specified column is located : " + metaData.getTableName(1)); } catch (Exception e) { // TODO
Auto-generated catch block //e.printStackTrace(); throw new
RuntimeException(e); }finally{ JdbcUtil.close(con, stmt, rs); } }
Operation results :
Gets the number of columns in the result set : 3 Gets the name of the specified column : id For the specified column SQL Type corresponds to java.sql.Types Class : 12 For the specified column SQL type : INT
Gets the specified column SQL Type corresponds to Java Type of : java.lang.Integer Gets the name of the table where the specified column is located : student
Code demonstration :
@Test public void resultSetMetaDataTest2(){ Connection con = null;
PreparedStatement stmt =null; ResultSet rs = null; try { // obtain Connection object con =
JdbcUtil.getConnection(); String sql ="select * from student";
// precompile sql sentence , obtain PreparedStatement object stmt = con.prepareStatement(sql); // implement sql rs =
stmt.executeQuery();// Get the result set meta object ResultSetMetaData metaData = rs.getMetaData();
// Iterate each row while(rs.next()){ // Get the number of columns int count = metaData.getColumnCount();
// ergodic , Gets the column name and value of each column for (int i = 0; i < count; i++) { // Get column name String columnName =
metaData.getColumnName(i+1); // Gets the value corresponding to the column name Object object =
rs.getObject(columnName);// output System.out.print(columnName + "=" + object + " "
); } System.out.println(); } } catch (Exception e) { // TODO Auto-generated
catch block //e.printStackTrace(); throw new RuntimeException(e); }finally{
JdbcUtil.close(con, stmt, rs); } }
Operation results :
id=1 sname= Chen Kefu gender= male id=2 sname= Sephirex Wang gender= male id=3 sname= Fang Wenshan gender= male id=4
sname= Deze gender= male id=5 sname= Haichao gender= male id=6 sname= Haiyang gender= male id=7 sname= Hai Rong
gender= male
application :

Code demonstration :
/** * @author chenkefo * Generic dao, All I wrote dao All inherit this class * This class defines 2 The same method : *
1. to update (insert/update/delete) * 2. query */ public class BaseDao { // Initialization parameters private
Connection conn;private PreparedStatement stmt; private ResultSet rs; /** *
Update general method */ public void update(String sql, Object[] paramsValue) { try { //
obtain Connection object conn = JdbcUtil.getConnection(); // precompile sql, obtain preparedstatement
stmt = conn.prepareStatement(sql);// Get the parameter metadata ParameterMetaData
parameterMetaData = stmt.getParameterMetaData();// Get the number of placeholders int count =
parameterMetaData.getParameterCount();// Place holder assignment if (paramsValue != null &&
paramsValue.length >0) { for (int i = 0; i < count; i++) { stmt.setObject(i + 1
, paramsValue[i]); } }// implement sql stmt.executeUpdate(); } catch (Exception e) { //
TODO Auto-generated catch block // e.printStackTrace(); throw new
RuntimeException(e); }finally { // close resource JdbcUtil.close(conn, stmt, null); } }
/** * General query method */ public <T> List<T> query(String sql, Object[] paramsValue,
Class<T> clazz) {try { // Create return collection List<T> list = new ArrayList<T>();
// obtain Connection object conn = JdbcUtil.getConnection(); // precompile sql, obtain preparedStatement
stmt = conn.prepareStatement(sql);// Get the parameter metaobject ParameterMetaData parameterMetaData
= stmt.getParameterMetaData();// Get the number of placeholders int count =
parameterMetaData.getParameterCount();// Assign a value to a placeholder if (paramsValue != null &&
paramsValue.length >0) { for (int i = 0; i < count; i++) { stmt.setObject(i + 1
, paramsValue[i]); } }// implement sql rs = stmt.executeQuery(); // Get the result set element
ResultSetMetaData metaData = rs.getMetaData();// Get the number of columns int columnCount =
metaData.getColumnCount();// Traversal result set while (rs.next()) { // create object T t =
clazz.newInstance();// Traverse each column for (int i = 0; i < columnCount; i++) { // Get the name of each column
String columnName = metaData.getColumnName(i +1); // By listing , Get value Object object =
rs.getObject(columnName);// adopt beanUtils Assign values to objects BeanUtils.copyProperty(t,
columnName, object); }// Add object to collection list.add(t); } // Return collection return list; } catch
(Exception e) {// TODO Auto-generated catch block // e.printStackTrace(); throw
new RuntimeException(); } finally { // close resource JdbcUtil.close(conn, stmt, rs); } } }

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network