JDBC study notes --ResultSetMetaData

1.why:

If only one result set, but do not know how many columns in the result set, the column names are what.

1) It should be used when writing generic query methods.

public <T> T get(Class<T> clazz, String sql, Object ... args){

}

2.what: Object describing the ResultSet.

3.how:

. ① ResultSetMetaData object obtained: calling the getMetaData the ResultSet () method 
ResultMetaData rsmd = resultSet.getMetaData ();
which has useful method ②.ResultsetMetaData:
> the getColumnCount int (): The results which set includes a column
> String getColumnLabel (int column) : Gets the designated column aliases, indexes start at 1

// get the result set aliases for each column
for (int I = 0; I <rsmd.getColumnCount (); I ++) { 
String columnLabel rsmd.getColumnLabel = (I + 1);
}

// sample code on the ResultSetMetaData
// 1 was ResultSetMetaData object.
ResultSetMetaData rsmd = Result. the getMetaData ();

the while (ResultSet.next ()) {
  . 2 // print column name of each column
  for (int I = 0; I <rsmd.getColumnCount (); I ++) {
    String columnLabel rsmd.getColumnLabel = (I + 1); // Get the specified column aliases
    System.out.println (columnLabel); // Print alias

    Object columnValue = resultSet.getObject (columnLabel); # the alias obtain the corresponding data in the database
    
    values.put (columnLabel, columnValue );
  }
}








Guess you like

Origin www.cnblogs.com/ifreewolf/p/11722461.html