----------------------------------------------
Some classes have the following code;
public class Test1 {
Testdao t=new Testdao ();
/*
* ResultSetMetaData: Describes the meta data in the result set.
* You can get the basic information in the result set: What columns, column names, column aliases are in the result set;
* Combined reflection to get a common query method
* */
@Test
public void Testresultsetmetadata () throws exception{
Connection Connection=null;
PreparedStatement Preparedstatement=null;
ResultSet Resultset=null;
try {
String sql= "Select Id,name,email,birth from Customer";
Connection=t.getconnection ();
Preparedstatement=connection.preparestatement (SQL);
Resultset=preparedstatement.executequery ();
ResultSetMetaData rsmd= (ResultSetMetaData) resultset.getmetadata ();
Get the number of columns
int N=rsmd.getcolumncount ();
SYSTEM.OUT.PRINTLN (n);
for (int i=0;i<n;i++) {
Get column names
String Columnname=rsmd.getcolumnname (i);
Get an alias to a column
String Columnlabel=rsmd.getcolumnlabel (i);
Get the value of the column
Object Columnvalues=resultset.getobject (ColumnLabel);
System.out.println (columnname+ "--" +columnlabel+ ":" +columnvalues ");
}
} catch (Exception e) {
E.printstacktrace ();
}finally {
T.close (Connection, PreparedStatement, ResultSet);
}
}
/*
* The DatabaseMetaData object is obtained on the connection object,
* The DatabaseMetaData class provides many ways to get information about a data source
* Learn More
* */
public void Testdatabasemetadata () throws exception{
Connection Connection=null;
ResultSet ResultSet =null;
try {
Connection=t.getconnection ();
DatabaseMetaData Data=connection.getmetadata ();
Can get some basic information about the database
Get the version number of the database
int version=data.getdatabasemajorversion ();
SYSTEM.OUT.PRINTLN (version);
Get the user name of the connected database
String User=data.getusername ();
SYSTEM.OUT.PRINTLN (user);
What databases are available in MySQL
Resultset=data.getcatalogs ();
while (Resultset.next ()) {
System.err.println (resultset.getstring (1));
}
} catch (Exception e) {
E.printstacktrace ();
}finally {
T.close (connection, NULL, resultSet);
}
}
}
ResultSetMetaData and DatabaseMetaData implement properties, property values, and acquisition of property values in the database.