Java Database Connectivity: Result Set Metadata

The ResuitSetMetaData object is used to retrieve information about the types and properties of the columns and other meta information about a ResultSet object. This is sometimes very useful, if you do not know much about the underlying database table. A ResultSetMetaData object is obtained using the getMetaData() method on the ResultSet object as follows:

ResultSet rs = stmt.executeQuery(“SELECT * FROM questions”);

ResultSetMetaData rsmd = rs.getMetaData();

It can be used to get some useful information such as number of rows, number of columns, column names, and their type. The following are some commonly used methods on the ResultSetMetaData object:

int getColumnCount()

Returns the number of columns in the result

String getColumnName(int)

Returns the name of a column in a result set. Requires an integer argument indicating the position of the column within the result set

int getColumnType(int)

Returns the type of the specified column in the form of java.sql.Types

String getColumnTypeName(int)

Returns the type of the specified column as a string

String getColumnClassName(int)

Returns the fully qualified Java type name of the specified column

int getPrecision(int)

Returns the number of decimal positions

int getScale(int)

Returns the number of digits after the decimal position

String getTableName(int)

Returns the name of the column’s underlying table

int isNullable(int)

Returns a constant indicating whether the specified column can have a NULL value The following JSP page shows how to retrieve meta information from a ResultSet object.

<!–ResultSetMetaData.jsp–>

<%@page import=”java.sql.*, java.lang.reflect.*”%>

<%

try {

Class.forName(“org.gjt.mm.mysql.Driver”);

String url = “jdbc:mysql://uroy:3306/test”;

Connection conn = DriverManager.getConnection(url, “root”, “nbuser”);

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(“SELECT * FROM questions”);

ResultSetMetaData rsmd = rs.getMetaData();

Object obj[] = new Object[1];

Method[] methods = rsmd.getClass().getDeclaredMethods();

out.println(“<table border=\”0\”><tr><td>Method Name</td>”);

for(int j = 0; j < rsmd.getColumnCount(); j++)

out.println(“<td>” + rsmd.getColumnName(j+1) + “</td>”);

out.println(“</tr>”);

for (int i = 0; i < methods.length; i++){

if(Modifier.isPublic(methods[i].getModifiers()))

if (methods[i].getParameterTypes().length == 1) {

if(!methods[i].getName().equals(“isWrapperFor”))

if(!methods[i].getName().equals(“unwrap”)) {

out.print(“<tr><td>” + methods[i].getName() + “</td>”);

for(int j=0;j<rsmd.getColumnCount();j++) { obj[0] = new

Integer(j+1);

out.print(“<td>” + methods[i].invoke(rsmd,obj) +

“</td>”);

}

out.println(“</tr>”);

}

}

}

out.println(“<table>”);

}catch(Exception e) {e.printStackTrace();}

%>

A sample result for MySQL database is shown in Table 22.5:

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *