Java 8 Statement - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Statement class.

This code example shows how to use the following methods:close, executeQuery
        Statement st = null;
        ResultSet rs = null;
 
        try { 
            st = con.createStatement();
            rs = st.executeQuery(query);
            if (rs != null && rs.next())
                result = rs.getString(1);
        } catch (Throwable t) {
            fail("it's impossible to execute query:" + query);
            t.printStackTrace();
        } finally { 
            if (rs != null) {
                try { 
                    rs.close();
                } catch (SQLException ignore) {
                    // Ignore 
                } 
            } 
            if (st != null) {

8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:executeQuery, setQueryTimeout
    try 
    { 
      stmt = conn.createStatement();
      if (timeoutSeconds > 0)
      { 
        stmt.setQueryTimeout(timeoutSeconds);
      } 
      rs = stmt.executeQuery(query);
 
      if (rs.next())
      { 
        res = rs.getLong(1);
        // NULL result is interpreted as 0 with getLong() 
        if (rs.wasNull())
        { 
          res = defaultVal;
        } 
      } 
    } 
    catch (SQLException e)

8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:executeQuery
		conn.close();
	} 
 
	public ResultSet executeQuery(String sql) throws SQLException {
		Statement stat = conn.createStatement();
		ResultSet rs = stat.executeQuery(sql);
		return rs;
	} 
 
	public int executeUpdate(String sql) throws SQLException {
		Statement stat = conn.createStatement();
		int affected_num = stat.executeUpdate(sql);
 
		if (stat != null) {
			stat.close();
		} 
		return affected_num;
	} 
} 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:execute
 
    public static void fillSalesPeopleTable(Connection conn)
            throws SQLException { 
        Statement statement = conn.createStatement();
 
        statement.execute(DatabaseCreator.INSERT_SALESPEOPLE1);
        statement.execute(DatabaseCreator.INSERT_SALESPEOPLE2);
        statement.execute(DatabaseCreator.INSERT_SALESPEOPLE3);
        statement.execute(DatabaseCreator.INSERT_SALESPEOPLE4);
        statement.execute(DatabaseCreator.INSERT_SALESPEOPLE5);
        statement.execute(DatabaseCreator.INSERT_SALESPEOPLE6);
    } 
 
    public static void fillCustomersTable(Connection conn) throws SQLException {
        Statement statement = conn.createStatement();
 
        statement.execute(DatabaseCreator.INSERT_CUSTOMERS1);
        statement.execute(DatabaseCreator.INSERT_CUSTOMERS2);
        statement.execute(DatabaseCreator.INSERT_CUSTOMERS3);
        statement.execute(DatabaseCreator.INSERT_CUSTOMERS4);
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:executeUpdate, setInt, setString, close
    } 
 
    private void addFlag(Connection connection, int newFlagId, String description, int lookupId) throws SQLException {
        PreparedStatement statement = connection.prepareStatement("insert into account_state_flag("
                + "  flag_id,lookup_id,status_id,flag_description,retain_flag)" + "values(?,?,?,?,?)"); 
        statement.setInt(1, newFlagId);
        statement.setInt(2, lookupId);
        statement.setInt(3, STATUS_10);
        statement.setString(4, description);
        boolean retain = false;
        statement.setInt(5, retain ? 1 : 0);
        statement.executeUpdate();
        statement.close();
    } 
} 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:close
 
    } 
 
    public void tearDown() throws Exception { 
        deleteTestTables(); 
        statement.close();
        conn.close();
        super.tearDown(); 
    } 
 
    public void createTestTables() { 
        try { 
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet userTab = meta.getTables(null, null, null, null);
 
            while (userTab.next()) {
                String tableName = userTab.getString("TABLE_NAME");
                if (tableName.equals(DatabaseCreator.PARENT_TABLE)) {
                    statement
                            .execute(DatabaseCreator.DROP_TABLE_PARENT);
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:executeUpdate, setInt, close
    } 
 
    private void addAction(Connection connection, int actionId, int lookupId) throws SQLException {
        PreparedStatement statement = connection
                .prepareStatement("insert into account_action(account_action_id,lookup_id) values (?,?)");
        statement.setInt(1, actionId);
        statement.setInt(2, lookupId);
        statement.executeUpdate();
        statement.close();
    } 
} 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:executeUpdate, setString, close, getGeneratedKeys
    protected int addLookupEntity(Connection connection, String name, String description)  throws SQLException {
       int newId = -1;
       PreparedStatement statement = connection.prepareStatement(
               "insert into lookup_entity(entity_id,entity_name,description) values(null,?,?)", 
                PreparedStatement.RETURN_GENERATED_KEYS);
       statement.setString(1, name);
       statement.setString(2, description);
       statement.executeUpdate();
       ResultSet keys = statement.getGeneratedKeys();
       keys.next();
       newId = Integer.parseInt(keys.getString(1));
       statement.close();
       return newId;
    } 
 
 
    public void setUpgradeContext(ApplicationContext upgradeContext) {
        this.upgradeContext = upgradeContext;
    } 
 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:executeQuery
		Connection con = DataSourceUtils.getConnection(getDataSource());
		Statement stmt = null;
		ResultSet rs = null;
		try { 
			stmt = con.createStatement();
			DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
			rs = stmt.executeQuery(getSequenceQuery());
			if (rs.next()) {
				return rs.getLong(1);
			} 
			else { 
				throw new DataAccessResourceFailureException("Sequence query did not return a result"); 
			} 
		} 
		catch (SQLException ex) {
			throw new DataAccessResourceFailureException("Could not obtain sequence value", ex);
		} 
		finally { 
			JdbcUtils.closeResultSet(rs);
			JdbcUtils.closeStatement(stmt);

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:executeQuery, setInt, close
    } 
 
    private short findLookupId(Connection connection) throws SQLException {
        PreparedStatement statement = connection.prepareStatement("select LOOKUP_ID "
                + "from financial_action where fin_action_id = ?"); 
        statement.setInt(1, action);
        ResultSet results = statement.executeQuery();
        if (results.next()) {
            short lookupId = results.getShort("lookup_id");
            statement.close();
            return lookupId;
        } else { 
            statement.close();
            throw new RuntimeException("unable to downgrade: no activity with id " + action);
        } 
    } 
 
    private void deleteFromAccountAction(Connection connection) throws SQLException {
        PreparedStatement statement = connection
                .prepareStatement("delete from financial_action where fin_action_id = ?");
7
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Java 8 Statement Questions & Answers:

See Code Examples for Java 8 Statement Methods: