Java Obtaining Result Set Metadata

are in t he set . However, be aware t hat if t he query generat es no result set , such as for an UPDATE st at em ent , t he value of description is None . Each elem ent of t he description t uple is anot her t uple t hat represent s t he m et adat a for t he corresponding colum n of t he result . There are seven m et adat a values per colum n; t he following code shows how t o access t hem and w hat t hey m ean: query = SELECT name, foods FROM profile print Query: , query cursor = conn.cursor cursor.execute query metadata information becomes available at this point ... print Number of rows:, cursor.rowcount if cursor.description == None: no result set ncols = 0 else: ncols = len cursor.description print Number of columns:, ncols if ncols == 0: print Note: query has no result set for i in range ncols: col_info = cursor.description[i] print name, then other information print --- Column d s --- i, col_info[0] print Type: , col_info[1] print Display size: , col_info[2] print Internal size:, col_info[3] print Precision: , col_info[4] print Scale: , col_info[5] print Nullable: , col_info[6] cursor.close The out put from t he program looks like t his: Query: SELECT name, foods FROM profile Number of rows: 10L Number of columns: 2 --- Column 0 name --- Type: 254 Display size: 7 Internal size: 20 Precision: 20 Scale: 0 Nullable: 0 --- Column 1 foods --- Type: 254 Display size: 21 Internal size: 42 Precision: 42 Scale: 0 Nullable: 1

9.3.7 Java

JDBC m akes result set m et adat a available t hrough a ResultSetMetaData obj ect , which you obt ain by calling t he getMetaData m et hod of your ResultSet obj ect . The m et adat a obj ect provides access t o several kinds of inform at ion. I t s getColumnCount m et hod ret urns t he num ber of colum ns in t he result set . Ot her t ypes of m et adat a, illust rat ed by t he following code, provide inform at ion about individual colum ns and t ake a colum n index as t heir argum ent . Not e t hat for JDBC, colum n indexes begin at 1, not 0, which differs from DBI , PHP, and DB- API . String query = SELECT name, foods FROM profile; System.out.println Query: + query; Statement s = conn.createStatement ; s.executeQuery query; ResultSet rs = s.getResultSet ; ResultSetMetaData md = rs.getMetaData ; metadata information becomes available at this point ... int ncols = md.getColumnCount ; System.out.println Number of columns: + ncols; if ncols == 0 System.out.println Note: query has no result set; for int i = 1; i = ncols; i++ column index values are 1-based { System.out.println --- Column + i + + md.getColumnName i + ---; System.out.println getColumnDisplaySize: + md.getColumnDisplaySize i; System.out.println getColumnLabel: + md.getColumnLabel i; System.out.println getColumnType: + md.getColumnType i; System.out.println getColumnTypeName: + md.getColumnTypeName i; System.out.println getPrecision: + md.getPrecision i; System.out.println getScale: + md.getScale i; System.out.println getTableName: + md.getTableName i; System.out.println isAutoIncrement: + md.isAutoIncrement i; System.out.println isNullable: + md.isNullable i; System.out.println isCaseSensitive: + md.isCaseSensitive i; System.out.println isSigned: + md.isSigned i; } rs.close ; s.close ; The out put from t he program looks like t his: Query: SELECT name, foods FROM profile Number of columns: 2 --- Column 1 name --- getColumnDisplaySize: 20 getColumnLabel: name getColumnType: 1 getColumnTypeName: CHAR getPrecision: 0 getScale: 0 getTableName: profile isAutoIncrement: false isNullable: 0 isCaseSensitive: true isSigned: false --- Column 2 foods --- getColumnDisplaySize: 42 getColumnLabel: foods getColumnType: 1 getColumnTypeName: CHAR getPrecision: 0 getScale: 0 getTableName: profile isAutoIncrement: false isNullable: 1 isCaseSensitive: true isSigned: false As wit h DBI , t he row count is not available direct ly; you m ust fet ch t he rows and count t hem . There act ually are several ot her JDBC result set m et adat a calls t han t he ones shown in t he preceding exam ple, but m any of t hem provide no useful inform at ion for MySQL. I f you want t o t ry t hem out , get a JDBC reference t o see what t he calls are and m odify t he program t o see what , if anyt hing, t hey ret urn.

9.4 Determining Presence or Absence of a Result Set