Wednesday, 1 October 2014

ResultSetMetaData

java.sql.ResultSetMetaData object represents types and properties of columns in a ResultSet object.

int getColumnCount() throws SQLException
Returns the number of columns in this ResultSet object.

String getColumnName(int column) throws SQLException
Get the designated column's name.

String getColumnClassName(int column) throws SQLException
Returns the java class name related to this ResultSet object column.

int getColumnDisplaySize(int column) throws SQLException
Returns column's normal maximum width in characters.

int getColumnType(int column) throws SQLException
Returns the designated column's SQL type.

Provide the link here (Miscellaneous topic 'ResultSet column type')

String getColumnTypeName(int column) throws SQLException
Returns designated column's database-specific type name.

String getColumnLabel(int column) throws SQLException
Returns the title of this column for use in printouts and displays.

String getTableName(int column) throws SQLException
Returns the column's table name.

String getSchemaName(int column) throws SQLException
Returns schema name or "" if not applicable

String getCatalogName(int column) throws SQLException
Returns the name of the catalog for the table in which the given column appears or "" if not applicable.

int getPrecision(int column) throws SQLException
Returns column's specified column size. For numeric data, this is the maximum precision. For character data, this is the length in characters.

int getScale(int column) throws SQLException
Return column's number of digits to right of the decimal point. 0 is returned for data types where the scale is not applicable.

boolean isAutoIncrement(int column) throws SQLException
Returns true if the designated column is automatically numbered, else false.

boolean isCaseSensitive(int column) throws SQLException
Returns if column's case matters,else false.

boolean isCurrency(int column) throws SQLException
Returns if designated column is a cash value, else false.

boolean isDefinitelyWritable(int column) throws SQLException
Returns true if write on the designated column will definitely succeed, else false.

int isNullable(int column) throws SQLException
Indicates the nullability of values in the designated column.

boolean isReadOnly(int column) throws SQLException
Returns true if the column is read-only, else false.

boolean isSearchable(int column) throws SQLException
Returns true if the given column can be used in a where clause, else false.

boolean isSigned(int column) throws SQLException
Returns true if the values in the designated column are signed numbers, else false.

boolean isWritable(int column) throws SQLException
Returns true if it is possible for a write on the designated column, else false.

/* Step 1: Import sql package */
import java.sql.*;
import java.sql.ResultSetMetaData;

public class SampleApp {
    
    /* Update username, password and driver details here */
    static Connection getConnection() throws ClassNotFoundException, SQLException{
         /* Step 2: Load Driver */
        System.out.println("Loading/Registering driver");  
       
        Class.forName("com.mysql.jdbc.Driver");
         
        /* Step 3: Open connection to database */
        System.out.println("Connecting to database");
        String url = "jdbc:mysql://localhost/world";
        String userName = "root";
        String pasword = "tiger";
        return DriverManager.getConnection(url, userName, pasword);
    }
    
    public static void main(String args[]) throws SQLException, ClassNotFoundException{      
        Connection conn = getConnection();
        conn.setAutoCommit(false);
          /* Create table employee */
        String query = "CREATE TABLE employee (id int, name varchar(30), PRIMARY KEY(id))";
        Statement stmt = conn.createStatement();
        stmt.execute(query);     
        
        /* Insert data to employee table */
        query = "INSERT INTO employee values(1, \"Krishna\")";
        stmt.execute(query);
        query = "INSERT INTO employee values(2, \"Arjun\")";
        stmt.execute(query);
        query = "INSERT INTO employee values(3, \"Joel\")";
        stmt.execute(query);
        
        
        query = "SELECT * FROM employee";
        ResultSet rs = stmt.executeQuery(query);
        ResultSetMetaData metaData= rs.getMetaData();
        
        System.out.println("Number Of Columns : " + metaData.getColumnCount());
        System.out.println("Column name : "+metaData.getColumnName(1));
        System.out.println("Column Class Name : "+metaData.getColumnClassName(1));
        System.out.println("Column Display size :"+metaData.getColumnDisplaySize(2));
        System.out.println("Column Type : " + metaData.getColumnType(1));
        System.out.println("Column type name : " + metaData.getColumnTypeName(1));
        System.out.println("Column label : " + metaData.getColumnLabel(1));
        System.out.println("Table Name : " + metaData.getTableName(1));
        System.out.println("Schema name : " + metaData.getSchemaName(1));
        System.out.println("Catalog name : "+ metaData.getCatalogName(1));
        System.out.println("Precision : " + metaData.getPrecision(2));       
        System.out.println("Scale : " + metaData.getScale(1));
        
        System.out.println("Ia Auto incremented : " + metaData.isAutoIncrement(1));
        System.out.println("Is Case sensitive matters : "+ metaData.isCaseSensitive(1));
        System.out.println("Is given column represents currency : " + metaData.isCurrency(1));
        System.out.println("Is write on the designated column will succeed : " + metaData.isDefinitelyWritable(1));
        System.out.println("Is the column nullable : " + metaData.isNullable(1));
        System.out.println("Is the column read only : " + metaData.isReadOnly(1));
        System.out.println("Is given column can be used in where clause : " + metaData.isSearchable(1));
        System.out.println("Is given column support signed numbers : " + metaData.isSigned(1));
        System.out.println("Is this column writable : " + metaData.isWritable(1));       
        
        System.out.println("Dropping table employee");
        query = "DROP TABLE employee";
        stmt.execute(query);
        
    rs.close();
        stmt.close();
        conn.close();
    }
}

Output
Loading/Registering driver
Connecting to database
Number Of Columns : 2
Column name : id
Column Class Name : java.lang.Integer
Column Display size :30
Column Type : 4
Column type name : INT
Column label : id
Table Name : employee
Schema name : 
Catalog name : world
Precision : 30
Scale : 0
Ia Auto incremented : false
Is Case sensitive matters : false
Is given column represents currency : false
Is write on the designated column will succeed : true
Is the column nullable : 0
Is the column read only : false
Is given column can be used in where clause : true
Is given column support signed numbers : true
Is this column writable : true
Dropping table employee



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment