Example usage for java.sql DatabaseMetaData getBestRowIdentifier

List of usage examples for java.sql DatabaseMetaData getBestRowIdentifier

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getBestRowIdentifier.

Prototype

ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable)
        throws SQLException;

Source Link

Document

Retrieves a description of a table's optimal set of columns that uniquely identifies a row.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    DatabaseMetaData meta = conn.getMetaData();
    // The '_' character represents any single character.
    // The '%' character represents any sequence of zero
    // or more characters.
    ResultSet rs = meta.getBestRowIdentifier(conn.getCatalog(), null, "survey",
            DatabaseMetaData.bestRowTemporary, false);
    while (rs.next()) {

        short actualScope = rs.getShort("SCOPE");
        String columnName = rs.getString("COLUMN_NAME");
        int dataType = rs.getInt("DATA_TYPE");
        String typeName = rs.getString("TYPE_NAME");
        int columnSize = rs.getInt("COLUMN_SIZE");
        short decimalDigits = rs.getShort("DECIMAL_DIGITS");
        short pseudoColumn = rs.getShort("PSEUDO_COLUMN");

        System.out.println("tableName=survey");
        System.out.println("scope=" + actualScope);
        System.out.println("columnName=" + columnName);
        System.out.println("dataType=" + dataType);
        System.out.println("typeName" + typeName);
        System.out.println("columnSize" + columnSize);
        System.out.println("decimalDigits" + decimalDigits);
        System.out.println("pseudoColumn" + pseudoColumn);
    }//  w w  w.j a  v  a2 s  . c  o  m

    st.close();
    conn.close();
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Reflect on the schema to find primary keys for the given table pattern.
 *//*from  w ww .j  a v  a2  s  . co m*/
protected PrimaryKey[] getPrimaryKeysFromBestRowIdentifier(DatabaseMetaData meta, DBIdentifier catalog,
        DBIdentifier schemaName, DBIdentifier tableName, Connection conn) throws SQLException {
    if (tableName == null)
        return null;

    beforeMetadataOperation(conn);
    ResultSet pks = null;
    try {
        pks = meta.getBestRowIdentifier(toDBName(catalog), toDBName(schemaName), toDBName(tableName), 0, false);

        List pkList = new ArrayList();
        while (pks != null && pks.next()) {
            PrimaryKey pk = new PrimaryKey();
            pk.setSchemaIdentifier(schemaName);
            pk.setTableIdentifier(tableName);
            pk.setColumnIdentifier(fromDBName(pks.getString("COLUMN_NAME"), DBIdentifierType.COLUMN));
            pkList.add(pk);
        }
        return (PrimaryKey[]) pkList.toArray(new PrimaryKey[pkList.size()]);
    } finally {
        if (pks != null)
            try {
                pks.close();
            } catch (Exception e) {
            }
    }
}