ResultSetMetaData gets the column names of the table

The editor continues to nag about this one , the ResultSetMetaData interface describes the information that belongs to the result set. The ResultSetMetaData object can be used to find information about the column type and attributes in the result set ResultSet. To get an instance of ResultSetMetaData, you can use the getMetaData method on the result set as follows:
ResultSetMetaData rsMetaData = resultSet. GetMetaData ();
Use the getColumnCount () method to find the number of columns in the result, use the getColumnName (int) method Can be listed.

在这里插入代码片
  Statement s=dbConn.createStatement();
         ResultSet r = s.executeQuery("Select * from student");
        ResultSetMetaData l=r.getMetaData();
        for(int i=1;i<=l.getColumnCount();i++)
        	System.out.printf("%-4s\t",l.getColumnName(i));
        System.out.println();
         while(r.next()) {
        	 for(int i=1;i<=l.getColumnCount();i++)
             	System.out.printf("%-4s\t",r.getObject(i));
        	 System.out.println();
         }
         dbConn.close();
Published 167 original articles · Like 16 · Visits 30,000+

Guess you like

Origin blog.csdn.net/feiqipengcheng/article/details/105444297