Package COM.ATGUIGU.JDBC;
Import java.sql.Connection;
Import Java.sql.DatabaseMetaData;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import Org.junit.Test;
Public class Metadatatest {
/**
* DatabaseMetaData is a metadata object that describes the database can be obtained by connection
*/
@Test
Public void Testdatabassmetadata () {
Connection Connection = null;
ResultSet Resultset=null;
try {
connection = jdbctools.getconnection ();
DatabaseMetaData data = Connection.getmetadata ();
//You can get some basic information of the repository itself
//database version number
int i=data.getdatabasemajorversion ();
System.out.println (i);
//database known user name
String username=data.getusername ();
System.out.println (UserName);
What databases are in//mysql
Resultset=data.getcatalogs ();
while (Resultset.next ()) {
System.out.println (resultset.getstring (1));
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
Jdbctools.release (null, NULL, connection);
}
}
/**
* Describes the metadata for the result set. You can get the basic information in the result set: What columns, column names, aliases, etc.
*/
@Test
public void Testresultsetmetadata () {
Connection connection=null;
PreparedStatement Preparedstatement=null;
ResultSet Resultset=null;
try{
Connection=jdbctools.getconnection ();
String sql= "Select Id,name,email,birth from Customers";
preparedstatement=connection.preparestatement (SQL);
Resultset=preparedstatement.executequery ();
//Got ResultSetMetaData
ResultSetMetaData resultsetmetadata=resultset.getmetadata ();
Gets the number of columns, the alias of the column, and the column name
for (int i=0;i<resultsetmetadata.getcolumncount (); i++) {
String columnlabel= Resultsetmetadata.getcolumnlabel (i+1);
System.out.println (ColumnLabel);
String Columnname=resultsetmetadata.getcolumnname (i+1);
System.out.println (columnname+ ":" +columnlabel);
}
}catch (Exception e) {
E.printstacktrace ();
} finally{
Jdbctools.release (ResultSet, PreparedStatement, connection);
}
}
}
Databasematadata\resultsetmetadata Data Metadata usage