/**
 * Usage: java TestTypeMap [ hostname ]
 * 
 * Hostname defaults to 'localhost'
 * Assumes user 'root', password 'root'
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class TestTypeMap {
    public static void main(String[] args) {
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	ResultSetMetaData rsm = null;
	String hostname = "localhost";
	if (args.length > 0) {
	    hostname = args[0];
	}
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
	    conn = DriverManager.getConnection(
	      "jdbc:mysql://"+hostname+"/test?user=root&password=root");

            stmt = conn.createStatement();

	    stmt.executeUpdate("DROP TABLE IF EXISTS test.test_data_types");

	    stmt.executeUpdate("CREATE TABLE test.test_data_types ( bi BIGINT, biu BIGINT UNSIGNED, i INT, iu INT UNSIGNED, mi MEDIUMINT, miu MEDIUMINT UNSIGNED, si SMALLINT, siu SMALLINT UNSIGNED, ti TINYINT, tiu TINYINT UNSIGNED, f FLOAT, ff DOUBLE, n NUMERIC(9,2), d DATE, dt DATETIME, dtp DATETIME(6), t TIME, tp TIME(6), ts TIMESTAMP, tsp TIMESTAMP(6), y YEAR, c CHAR(255), bc BINARY(255), v VARCHAR(255), lv VARCHAR(32767), vb VARBINARY(255), b1 BIT(1), b2 BIT(64), lb LONGBLOB, mb MEDIUMBLOB, b BLOB, tb TINYBLOB, lt LONGTEXT, mt MEDIUMTEXT, tx TEXT, tt TINYTEXT, e ENUM('A', 'B', 'C'), s SET('A', 'B', 'C'), PRIMARY KEY (bi) )");

	    stmt.executeUpdate("INSERT INTO test.test_data_types SET bi = 123, biu = 123, i  = 123, iu = 123, mi = 123, miu = 123, si = 123, siu = 123, ti = 123, tiu = 123, f  = 3.14, ff = 3.14, n  = 3.14, d  = CURRENT_DATE(), dt = CURRENT_TIMESTAMP(), dtp = CURRENT_TIMESTAMP(6), t  = CURRENT_TIME(), tp = CURRENT_TIME(6), ts = CURRENT_TIMESTAMP(), tsp = CURRENT_TIMESTAMP(6), y  = 2013, c  = 'char', v  = 'varchar', lv = 'longvarchar', b1 = 1, b2 = 1, lb = 'longblob', mb = 'mediumblob', b  = 'blob', tb = 'tinyblob', lt = 'longtext', mt = 'mediumtext', tx = 'text', tt = 'tinytext', e  = 'A', s  = 'A,B'");

	    rs = stmt.executeQuery("SELECT * FROM test.test_data_types");
	    rsm = rs.getMetaData();
	    while (rs.next()) {
		for (int i=1; i<=rsm.getColumnCount(); ++i) {
		    try {
			System.out.println(
			    "Column="+i+
			    " ColumnName="+rsm.getColumnName(i)+
			    " SQLType="+rsm.getColumnType(i)+
			    " SQLTypeName="+rsm.getColumnTypeName(i)+
			    " Precision="+rsm.getPrecision(i)+
			    " Scale="+rsm.getScale(i)+
			    " MetadataClassName="+rsm.getColumnClassName(i)
			);
			if (rsm.getColumnType(i) > 0 || rsm.getColumnClassName(i) == "java.lang.String") {
			    Object o = rs.getObject(i);
			    System.out.println(
				" ObjectClassName="+o.getClass().getName()+
				" Value=\""+o.toString()+"\""
			    );
			}
			System.out.println();
		    } catch (SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		    } catch (Exception ex) {
			System.out.println("Exception Type: " + ex.getClass().getName());
			System.out.println("Exception: " + ex.getMessage());
		    }
		}
	    }
        } catch (SQLException ex) {
	    System.out.println("SQLException: " + ex.getMessage());
	    System.out.println("SQLState: " + ex.getSQLState());
	    System.out.println("VendorError: " + ex.getErrorCode());
        } catch (Exception ex) {
	    System.out.println("Exception Type: " + ex.getClass().getName());
	    System.out.println("Exception: " + ex.getMessage());
        } finally {
	    if (rs != null) {
		try {
		    rs.close();
		} catch (SQLException sqlEx) { } // ignore

		rs = null;
	    }

	    if (stmt != null) {
		try {
		    stmt.close();
		} catch (SQLException sqlEx) { } // ignore

		stmt = null;
	    }
	}
    }
}
