Example usage for java.sql ResultSet getString

List of usage examples for java.sql ResultSet getString

Introduction

In this page you can find the example usage for java.sql ResultSet getString.

Prototype

String getString(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

Usage

From source file:PrintResultSet.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from employee");
    ResultSetMetaData md = rs.getMetaData();

    int nColumns = md.getColumnCount();
    for (int i = 1; i <= nColumns; i++) {

        System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t"));
    }// w  w  w. j  av  a 2  s .  com
    while (rs.next()) {
        for (int i = 1; i <= nColumns; i++) {
            System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t"));
        }
    }
}

From source file:PrintIndexedResultSet.java

public static void main(String args[]) throws Exception {
    String query = "SELECT STATE, COUNT(STATE) FROM MEMBER_PROFILES GROUP BY STATE";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Members");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("CREATE INDEX STATE_INDEX ON MEMBER_PROFILES(STATE)");

    java.util.Date startTime = new java.util.Date();

    ResultSet rs = stmt.executeQuery(query);
    ResultSetMetaData md = rs.getMetaData();

    int nColumns = md.getColumnCount();
    for (int i = 1; i <= nColumns; i++) {
        System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t"));
    }//from  w w  w . j  av  a  2 s  .co  m

    while (rs.next()) {
        for (int i = 1; i <= nColumns; i++) {
            System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t"));
        }
    }
    java.util.Date endTime = new java.util.Date();
    long elapsedTime = endTime.getTime() - startTime.getTime();
    System.out.println("Elapsed time: " + elapsedTime);

    stmt.executeUpdate("DROP INDEX MEMBER_PROFILES.STATE_INDEX");
}

From source file:DeliverWork.java

public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
    JdbcFactory jdbcFactoryChanye = new JdbcFactory(
            "jdbc:mysql://127.0.0.1:3306/fp_guimin?useUnicode=true&characterEncoding=UTF-8", "guimin_db",
            "guimin@98fhi3p2hUFHfdfoi", "com.mysql.jdbc.Driver");
    connection = jdbcFactoryChanye.getConnection();
    Map<String, String> map = new HashMap();

    try {//  w  w  w . j  a v a2  s. co m
        PreparedStatement ps = connection.prepareStatement(selectAccount);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            map.put(resultSet.getString(1), resultSet.getString(2));
        }
        ps.close();
        map.forEach((k, v) -> {
            syncTheFile(k, v);
        });
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:GetColumnPrivilegesOracle.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from w  w w.j  av a  2  s  . c o  m*/

    conn = getConnection();
    String catalogPattern = conn.getCatalog();
    String schemaPattern = "SYSTEM";
    String tableNamePattern = "HELP";

    ResultSet privileges = null;
    DatabaseMetaData meta = conn.getMetaData();

    // The '_' character represents any single character.
    // The '%' character represents any sequence of zero or more characters.
    privileges = meta.getTablePrivileges(catalogPattern, schemaPattern, tableNamePattern);
    while (privileges.next()) {

        String catalog = privileges.getString("TABLE_CAT");
        String schema = privileges.getString("TABLE_SCHEM");
        String tableName = privileges.getString("TABLE_NAME");
        String privilege = privileges.getString("PRIVILEGE");
        String grantor = privileges.getString("GRANTOR");
        String grantee = privileges.getString("GRANTEE");
        String isGrantable = privileges.getString("IS_GRANTABLE");

        System.out.println("table name:" + tableName);
        System.out.println("catalog:" + catalog);
        System.out.println("schema:" + schema);
        System.out.println("privilege:" + privilege);
        System.out.println("grantor:" + grantor);
        System.out.println("isGrantable:" + isGrantable);
        System.out.println("grantee:" + grantee);
        conn.close();

    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    ResultSet rs = DriverManager.getConnection("jdbc:odbc:employee_xls").createStatement()
            .executeQuery("Select * from [Sheet1$]");

    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();

    System.out.println("No of cols " + numberOfColumns);
    while (rs.next()) {
        for (int i = 1; i <= numberOfColumns; i++) {
            String columnValue = rs.getString(i);
            System.out.println(columnValue);
        }/*from   w w w. j  a  va2  s  .c  o m*/
    }
    rs.close();
}

From source file:GetNumberOfRowsScrollableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;/*from www. jav a  2 s.c  o m*/
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        rs = stmt.executeQuery(query);
        // extract data from the ResultSet scroll from top
        while (rs.next()) {
            String id = rs.getString(1);
            System.out.println("id=" + id);
        }
        // move to the end of the result set
        rs.last();
        // get the row number of the last row which is also the row count
        int rowCount = rs.getRow();
        System.out.println("rowCount=" + rowCount);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.alifi.jgenerator.spring.SpringGeneratorFactory.java

public static void main(String[] args) {
    @SuppressWarnings("unused")
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/SpringContext.xml");

    for (DataSourceMap m : DataSourceUtil.getDataSourceMap()) {

        DataSource ds = m.getDataSource();
        try {/*from  w  w w  .j  a v  a 2s. co m*/
            Connection con = ds.getConnection();
            DatabaseMetaData dmd = con.getMetaData();
            String catalog = con.getCatalog();
            String schemaPattern = dmd.getSchemaTerm();
            System.out.println("Catalog:" + con.getCatalog() + " schemaPattern:" + schemaPattern);
            ResultSet rs = dmd.getTables(catalog, schemaPattern, "time_zone_transition", null);

            Statement s = null;
            ResultSet xrs = null;
            try {
                s = con.createStatement();
                xrs = s.executeQuery("select * from time_zone_transition");
                if (xrs.next()) {
                    p("xxxxxxxxxxxxxxxxxxxxxxxx:" + xrs.getString(1));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
            }

            while (rs.next()) {
                p("-----------------------");
                p("TABLE_CAT  :" + rs.getString("TABLE_CAT"));
                p("TABLE_SCHEM:" + rs.getString("TABLE_SCHEM"));
                p("TABLE_NAME :" + rs.getString("TABLE_NAME"));
                p("TABLE_TYPE :" + rs.getString("TABLE_TYPE"));
                p("REMARKS:   " + rs.getString("REMARKS"));

                p("11111111111111111:" + rs.getMetaData().getColumnClassName(1));
                // 
                ResultSet pkeyRs = dmd.getPrimaryKeys(catalog, schemaPattern, rs.getString("TABLE_NAME"));
                while (pkeyRs.next()) {
                    p("M-------------M");
                    p("------COLUMN_NAME:" + pkeyRs.getString("COLUMN_NAME"));
                    p("------KEY_SEQ:" + pkeyRs.getString("KEY_SEQ"));
                    p("------PK_NAME:" + pkeyRs.getString("PK_NAME"));
                }
                // 
                ResultSet rss = dmd.getColumns(catalog, schemaPattern, rs.getString("TABLE_NAME"), null);
                int cCount = rss.getMetaData().getColumnCount();
                for (int i = 1; i <= cCount; i++) {
                    p("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
                    p("       getColumnClassName:" + rss.getMetaData().getColumnClassName(i));
                    p("       getColumnLabel:" + rss.getMetaData().getColumnLabel(i));
                    p("       getColumnName:" + rss.getMetaData().getColumnName(i));
                    p("       getColumnTypeName:" + rss.getMetaData().getColumnTypeName(i));
                    p("       getColumnType:" + ColumnTypes.getType(rss.getMetaData().getColumnType(i)));
                }
            }
            rs = dmd.getTableTypes();

            while (rs.next()) {
                p("=========================");
                p("TABLE_TYPE: " + rs.getString("TABLE_TYPE"));
            }
            // ResultSetMetaData rsmd = rs.getMetaData();

            // int numberOfColumns = rsmd.getColumnCount();
            // for (int i = 1; i <= numberOfColumns; i++)
            // p(rsmd.getColumnName(i));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        p(m.toString());
    }
}

From source file:TypeConcurrency.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;// ww  w. j  a  va2  s  .  c  o  m
    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.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

        ResultSet srs = stmt.executeQuery("SELECT * FROM COFFEES");

        int type = srs.getType();

        System.out.println("srs is type " + type);

        int concur = srs.getConcurrency();

        System.out.println("srs has concurrency " + concur);
        while (srs.next()) {
            String name = srs.getString("COF_NAME");
            int id = srs.getInt("SUP_ID");
            float price = srs.getFloat("PRICE");
            int sales = srs.getInt("SALES");
            int total = srs.getInt("TOTAL");
            System.out.print(name + "   " + id + "   " + price);
            System.out.println("   " + sales + "   " + total);
        }

        srs.close();
        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("-----SQLException-----");
        System.err.println("SQLState:  " + ex.getSQLState());
        System.err.println("Message:  " + ex.getMessage());
        System.err.println("Vendor:  " + ex.getErrorCode());
    }
}

From source file:DemoScrollableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;//from   w ww .  ja va  2  s  .  c o m
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id, name from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery(query);
        // extract data from the ResultSet scroll from top
        while (rs.next()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
        // scroll from the bottom
        rs.afterLast();
        while (rs.previous()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // release database resources
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

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')");

    ResultSet privileges = null;
    DatabaseMetaData meta = conn.getMetaData();
    // The '_' character represents any single character.
    // The '%' character represents any sequence of zero
    // or more characters.
    privileges = meta.getTablePrivileges(conn.getCatalog(), "%", "survey");
    while (privileges.next()) {
        String catalog = privileges.getString("TABLE_CAT");
        String schema = privileges.getString("TABLE_SCHEM");
        String tableName = privileges.getString("TABLE_NAME");
        String privilege = privileges.getString("PRIVILEGE");
        String grantor = privileges.getString("GRANTOR");
        String grantee = privileges.getString("GRANTEE");
        String isGrantable = privileges.getString("IS_GRANTABLE");

        System.out.println("table name:" + tableName);
        System.out.println("catalog:" + catalog);
        System.out.println("schema:" + schema);
        System.out.println("privilege:" + privilege);
        System.out.println("grantor:" + grantor);
        System.out.println("isGrantable:" + isGrantable);
        System.out.println("grantee:" + grantee);
    }/*from w  w w.j a v  a  2 s.  c  om*/

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