ResultSetMetaData and DatabaseMetaData implement properties, property values, and acquisition of property values in the database.

Source: Internet
Author: User


----------------------------------------------
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.

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.