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:MainClass.java

public static void readDatabase() {
    String data = "jdbc:derby:presidents";
    try {/*  w  w  w . jav  a2 s.  c  o  m*/
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection conn = DriverManager.getConnection(data, "", "");
        Statement st = conn.createStatement();
        ResultSet rec = st.executeQuery("SELECT * FROM contacts ORDER BY name");
        while (rec.next()) {
            System.out.println(
                    rec.getString("name") + "\n" + rec.getString("address1") + "\n" + rec.getString("address2")
                            + "\n" + rec.getString("phone") + "\n" + rec.getString("email") + "\n");
        }
        st.close();
    } catch (Exception e) {
        System.out.println("Error - " + e.toString());
    }
}

From source file:TestDB.java

 /**
 * Runs a test by creating a table, adding a value, showing the table contents, and removing the
 * table./*from  www  . j  ava2  s  . co  m*/
 */
public static void runTest() throws SQLException, IOException
{
   Connection conn = getConnection();
   try
   {
      Statement stat = conn.createStatement();

      stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");
      stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");

      ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
      if (result.next())
         System.out.println(result.getString(1));
      result.close();
      stat.executeUpdate("DROP TABLE Greetings");
   }
   finally
   {
      conn.close();
   }
}

From source file:com.dbconnection.DataSourceConnection.java

private static String query(Connection connection) throws SQLException {

    String query = "";
    Statement statement = connection.createStatement();

    ResultSet resultSet = statement.executeQuery("SELECT * FROM BOOK");

    while (resultSet.next()) {
        int id = resultSet.getInt("id");
        String title = resultSet.getString("title");

        query = query + id + "\t" + title + "\n";
    }//from   ww w. j av a 2s. com
    return query;
}

From source file:PooledConnectionExample.java

private static void printEmployee(ResultSet resultSet) throws SQLException {
    System.out.print(resultSet.getInt("employee_id"));
    System.out.print(", ");
    System.out.print(resultSet.getString("last_name"));
    System.out.print(", ");
    System.out.print(resultSet.getString("first_name"));
    System.out.print(", ");
    System.out.println(resultSet.getString("email"));
}

From source file:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findCoffee(int coffeeId, String[] coffeeDescription) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try {//  w w w .  j  a  v a 2 s  .co m
        connection = DriverManager.getConnection("jdbc:default:connection");
        String sql = "SELECT * FROM COFFEE_BEVERAGES WHERE ID = ? ";
        statement = connection.prepareStatement(sql);
        statement.setLong(1, coffeeId);

        ResultSet resultset = statement.executeQuery();
        resultset.next();
        coffeeDescription[0] = resultset.getString("COFFEE_DESCRIPTION");

    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeConnection(connection);
    }

}

From source file:Main.java

private static List<Person> findAll(Connection conn) throws SQLException {
    List<Person> rows = new ArrayList<Person>();
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("select * from people;");
    while (rs.next()) {
        rows.add(new Person(rs.getString("name"), rs.getString("occupation")));
    }//from  w w w  . j  av  a2  s  .  c o m
    close(stat);
    close(rs);
    return rows;
}

From source file:tds.assessment.repositories.impl.StrandQueryRepositoryImpl.java

private static Strand buildStrandFromResultSet(final ResultSet rs) throws SQLException {
    return new Strand.Builder().withName(rs.getString("name")).withKey(rs.getString("_key"))
            .withMinItems(rs.getInt("minitems")).withMaxItems(rs.getInt("maxitems"))
            // calling getObject() and casting to Float because .getFloat() defaults to 0 if null
            .withAdaptiveCut((Float) rs.getObject("adaptivecut")).withSegmentKey(rs.getString("segmentKey"))
            .withStrictMax(rs.getBoolean("isstrictmax")).withBpWeight(rs.getFloat("bpweight"))
            .withStartInfo((Float) rs.getObject("startInfo")).withScalar((Float) rs.getObject("scalar"))
            .withPrecisionTarget((Float) rs.getObject("precisionTarget"))
            .withPrecisionTargetMetWeight((Float) rs.getObject("precisionTargetMetWeight"))
            .withPrecisionTargetNotMetWeight((Float) rs.getObject("precisionTargetNotMetWeight")).build();
}

From source file:Main.java

public static String QueryVcTable2(Statement stmt, String app_name) {
    String sql_1 = "SELECT app_versioncode FROM app_info.`msp_table_8.12_copy` WHERE app_name='" + app_name
            + "'";
    ResultSet result = null;
    String v = null;//from   www . j a  va2s . c  o  m
    try {
        result = stmt.executeQuery(sql_1);
        while (result.next()) {
            v = result.getString(1);
        }
        if (v != null)
            return v;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.kaidad.utilities.MBTilesBase64Converter.java

private static void encodeTileData(final JdbcTemplate c) {
    c.query("SELECT tile_id, tile_data FROM images", new ResultSetExtractor<Object>() {
        @Override/*from w ww. ja  va  2s.  c  o  m*/
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
            while (rs.next()) {
                String tileId = rs.getString("tile_id");
                byte[] imageData = rs.getBytes("tile_data");
                addTileDataToStringTable(c, tileId, BaseEncoding.base64().encode(imageData));
            }
            System.out.println("Operation done successfully");
            return null;
        }
    });
}

From source file:de.thejeterlp.bukkit.login.SQLAccount.java

protected static Account convert(UUID uuid) throws SQLException {
    checkReflection();/*from   w  w w.  j  a va  2  s  .  com*/
    Validate.notNull(uuid, "uuid cannot be null!");
    PreparedStatement st = Login.getInstance().getDB()
            .getPreparedStatement("SELECT * FROM `" + Statics.USER_TABLE + "` WHERE `uuid` = ? LIMIT 1;");
    st.setString(1, uuid.toString());
    ResultSet rs = st.executeQuery();
    while (rs.next()) {
        int id = rs.getInt("id");
        Login.getInstance().getDB().closeResultSet(rs);
        Login.getInstance().getDB().closeStatement(st);
        PreparedStatement sta = Login.getInstance().getDB().getPreparedStatement(
                "SELECT * FROM `" + Statics.PASSWORD_TABLE + "` WHERE `userID` = ? LIMIT 1;");
        sta.setInt(1, id);
        ResultSet rset = sta.executeQuery();
        while (rset.next()) {
            String hash = rset.getString("password");
            Login.getInstance().getDB().closeResultSet(rset);
            Login.getInstance().getDB().closeStatement(sta);
            return new Account(id, uuid, hash);
        }
    }
    return null;
}