20-CS-694 Advanced Programming Techniques Spring 2012
JDBC

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next      PostgreSQL Docs      MySQL Docs      PostgreSQL      MySQL      All lectures

RS Meta Data Methods

import java.sql.*;
     
public class RSMetaDataMethods {
   public static void main(String args[]) {
      String url = "jdbc:mySubprotocol:myDataSource";
      Connection con;
      Statement stmt;
   
      try {
         Class.forName("myDriver.ClassName");
      }
      catch(java.lang.ClassNotFoundException e) {
         System.err.print("ClassNotFoundException: "); 
         System.err.println(e.getMessage());
      }

      try {
         con = DriverManager.getConnection(url, "myLogin", "myPassword");
   
         stmt = con.createStatement();                     
   
         ResultSet rs = stmt.executeQuery("select * from COFFEES");
         ResultSetMetaData rsmd = rs.getMetaData();
   
         int numberOfColumns = rsmd.getColumnCount();
         for (int i = 1; i <= numberOfColumns; i++) {
            String colName = rsmd.getColumnName(i);
            String tableName = rsmd.getTableName(i);
            String name = rsmd.getColumnTypeName(i);
            boolean caseSen = rsmd.isCaseSensitive(i);
            boolean writable = rsmd.isWritable(i);
            System.out.println("Information for column " + colName);
            System.out.println("    Column is in table " + tableName);
            System.out.println("    DBMS name for type is " + name);
            System.out.println("    Is case sensitive:  " + caseSen);
            System.out.println("    Is possibly writable:  " + writable);
            System.out.println("");
         }
   
         while (rs.next()) {
            for (int i = 1; i<=numberOfColumns; i++) {
               String s = rs.getString(i);
               System.out.print(s + "  ");
            }
            System.out.println("");
         }
   
         stmt.close();
         con.close();
      
      }
      catch(SQLException ex) {
         System.err.println("SQLException: " + ex.getMessage());
      }
   }
}