Example usage for org.apache.commons.dbutils QueryRunner update

List of usage examples for org.apache.commons.dbutils QueryRunner update

Introduction

In this page you can find the example usage for org.apache.commons.dbutils QueryRunner update.

Prototype

public int update(String sql) throws SQLException 

Source Link

Document

Executes the given INSERT, UPDATE, or DELETE SQL statement without any replacement parameters.

Usage

From source file:gr.osmosis.rcpsamples.contact.db.derby.DerbyContactsDAO.java

public boolean updateContact(Contact contact) {
    if (contact == null) {
        throw new NullPointerException("contact parameter");
    }/*w  ww. j ava 2 s. c o m*/

    int rows = 0;

    try {
        StringBuffer sbUpdate = new StringBuffer();

        sbUpdate.append("UPDATE ");
        sbUpdate.append(ContactsConstants.CONTACTS_TABLE_NAME);
        sbUpdate.append(" SET ");
        sbUpdate.append(ContactsConstants.CONTACTS_COL_FNAME + " = '" + contact.getFname() + "' , ");
        sbUpdate.append(ContactsConstants.CONTACTS_COL_LNAME + " = '" + contact.getLname() + "' , ");
        sbUpdate.append(ContactsConstants.CONTACTS_COL_PHONE + " = '" + contact.getPhone() + "' , ");
        sbUpdate.append(ContactsConstants.CONTACTS_COL_ADDRESS + " = '" + contact.getAddress() + "' , ");
        sbUpdate.append(ContactsConstants.CONTACTS_COL_CITY + " = '" + contact.getCity() + "' , ");
        sbUpdate.append(ContactsConstants.CONTACTS_COL_ZIP + " = '" + contact.getZip() + "'  ");

        sbUpdate.append(" WHERE " + ContactsConstants.CONTACTS_COL_ID + " = " + contact.getId());

        DataSource d = DerbyDAOFactory.getDataSource();
        QueryRunner run = new QueryRunner(d);

        rows = run.update(sbUpdate.toString());

        if (rows != 1) {
            throw new SQLException("executeUpdate return value: " + rows);
        }

    } catch (SQLException ex) {
        // throw new DAORuntimeException(ex);
        System.out.println(ex.getMessage());
        return false;
    }

    return true;

}

From source file:gr.osmosis.rcpsamples.contact.db.derby.DerbyContactsDAO.java

public boolean createContact(Contact contact) {
    if (contact == null) {
        throw new NullPointerException("contact parameter");
    }//  w  ww.j av a  2 s .  c om

    int rows = 0;

    try {
        StringBuffer sbInsert = new StringBuffer();

        sbInsert.append("INSERT INTO ");
        sbInsert.append(ContactsConstants.CONTACTS_TABLE_NAME);
        sbInsert.append("(");
        sbInsert.append(ContactsConstants.CONTACTS_COL_FNAME + ", ");
        sbInsert.append(ContactsConstants.CONTACTS_COL_LNAME + ", ");
        sbInsert.append(ContactsConstants.CONTACTS_COL_PHONE + ", ");
        sbInsert.append(ContactsConstants.CONTACTS_COL_ADDRESS + ", ");
        sbInsert.append(ContactsConstants.CONTACTS_COL_CITY + ", ");
        sbInsert.append(ContactsConstants.CONTACTS_COL_ZIP);
        sbInsert.append(")");
        sbInsert.append(" VALUES (");
        sbInsert.append(" '" + contact.getFname() + "' , ");
        sbInsert.append(" '" + contact.getLname() + "' , ");
        sbInsert.append(" '" + contact.getPhone() + "' , ");
        sbInsert.append(" '" + contact.getAddress() + "' , ");
        sbInsert.append(" '" + contact.getCity() + "' , ");
        sbInsert.append(" '" + contact.getZip() + "'  ");
        sbInsert.append(")");

        DataSource d = DerbyDAOFactory.getDataSource();
        QueryRunner run = new QueryRunner(d);

        rows = run.update(sbInsert.toString());

        if (rows != 1) {
            throw new SQLException("executeUpdate return value: " + rows);
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
        return false;
    }

    return true;

}

From source file:com.gs.obevo.dbmetadata.impl.dialects.AbstractDbMetadataManagerIT.java

@Before
public void setup() throws Exception {
    DataSource ds = getDataSource();
    mgr = createMetadataManager();//  w  w  w .  j a va2s  .c  o  m
    mgr.setDataSource(ds);

    QueryRunner jdbc = new QueryRunner(ds, isPmdKnownBroken());

    setCurrentSchema(jdbc);

    for (String sql : splitSql(getDropSqlFile())) {
        try {
            jdbc.update(sql);
        } catch (Exception ignore) {
            LOG.info("Ignoring error here for dropping tables (simplest way to do this), {}",
                    ignore.getMessage());
        }
    }

    for (String sql : splitSql(getAddSqlFile())) {
        jdbc.update(sql);
    }
}

From source file:jdao.JDAO.java

/**
 * executes a statment and returns the number of rows effected.
 * <p>/*  w  ww  .  java  2 s. co  m*/
 * if the only (first) argument is a map, the sql string is expected to have "?{field-name}" named parameters
 * <p>
 * if the only (first) argument is a list (collection), it is take as the list of arguments.
 *
 */
public static int update(int dbType, Connection conn, QueryRunner ds, String sql, Object... args)
        throws Exception {
    if (args == null) {
        if (conn == null) {
            return ds.update(sql);
        } else {
            return ds.update(conn, sql);
        }
    } else if (args.length > 0 && args[0] instanceof Map) {
        List nArgs = new Vector();
        sql = preparseParameters(dbType, sql, nArgs, (Map) args[0]);
        if (conn == null) {
            return ds.update(sql, nArgs.toArray());
        } else {
            return ds.update(conn, sql, nArgs.toArray());
        }
    } else if (args.length > 0 && args[0] instanceof Collection) {
        if (conn == null) {
            return ds.update(sql, ((Collection) args[0]).toArray());
        } else {
            return ds.update(conn, sql, ((Collection) args[0]).toArray());
        }
    } else {
        if (conn == null) {
            return ds.update(sql, args);
        } else {
            return ds.update(conn, sql, args);
        }
    }
}

From source file:io.apiman.gateway.engine.jdbc.JdbcInitializer.java

/**
 * Do the initialization work./*w w w. j  a v  a 2  s  . c  o m*/
 */
@SuppressWarnings("nls")
private void doInit() {
    QueryRunner run = new QueryRunner(ds);
    Boolean isInitialized;

    try {
        isInitialized = run.query("SELECT * FROM gw_apis", new ResultSetHandler<Boolean>() {
            @Override
            public Boolean handle(ResultSet rs) throws SQLException {
                return true;
            }
        });
    } catch (SQLException e) {
        isInitialized = false;
    }

    if (isInitialized) {
        System.out.println("============================================");
        System.out.println("Apiman Gateway database already initialized.");
        System.out.println("============================================");
        return;
    }

    ClassLoader cl = JdbcInitializer.class.getClassLoader();
    URL resource = cl.getResource("ddls/apiman-gateway_" + dbType + ".ddl");
    try (InputStream is = resource.openStream()) {
        System.out.println("=======================================");
        System.out.println("Initializing apiman Gateway database.");
        DdlParser ddlParser = new DdlParser();
        List<String> statements = ddlParser.parse(is);
        for (String sql : statements) {
            System.out.println(sql);
            run.update(sql);
        }
        System.out.println("=======================================");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:io.apiman.manager.api.jpa.JpaStorageInitializer.java

/**
 * Called to initialize the database./*from ww  w.  j a v  a 2  s. c  o  m*/
 */
@SuppressWarnings("nls")
public void initialize() {
    QueryRunner run = new QueryRunner(ds);
    Boolean isInitialized;

    try {
        isInitialized = run.query("SELECT * FROM apis", new ResultSetHandler<Boolean>() {
            @Override
            public Boolean handle(ResultSet rs) throws SQLException {
                return true;
            }
        });
    } catch (SQLException e) {
        isInitialized = false;
    }

    if (isInitialized) {
        System.out.println("============================================");
        System.out.println("Apiman Manager database already initialized.");
        System.out.println("============================================");
        return;
    }

    ClassLoader cl = JpaStorageInitializer.class.getClassLoader();
    URL resource = cl.getResource("ddls/apiman_" + dbType + ".ddl");
    try (InputStream is = resource.openStream()) {
        System.out.println("=======================================");
        System.out.println("Initializing apiman Manager database.");
        DdlParser ddlParser = new DdlParser();
        List<String> statements = ddlParser.parse(is);
        for (String sql : statements) {
            System.out.println(sql);
            run.update(sql);
        }
        System.out.println("=======================================");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:azkaban.executor.JdbcExecutorLoader.java

public void updateExecutableJobsOnStartUp() throws ExecutorManagerException {
    final String UPDATE_EXECUTION_JOBS = "update execution_jobs join execution_flows on"
            + " execution_jobs.exec_id = execution_flows.exec_id " + "set execution_jobs.status = 70 where "
            + "execution_jobs.status = 30 and execution_flows.status = 70";

    QueryRunner runner = createQueryRunner();
    try {/*from   w w  w . j  a va  2  s  .co m*/
        runner.update(UPDATE_EXECUTION_JOBS);
    } catch (SQLException e) {
        throw new ExecutorManagerException("Error updating jobs ", e);
    }
}

From source file:org.apache.lens.server.query.LensServerDAO.java

/**
 * Drop finished queries table./*from   ww w .j  a va2  s .c  o m*/
 */
public void dropFinishedQueriesTable() {
    QueryRunner runner = new QueryRunner(ds);
    try {
        runner.update("drop table finished_queries");
    } catch (SQLException e) {
        log.error("SQL exception while dropping finished queries table.", e);
    }
}

From source file:org.apache.lens.server.query.LensServerDAO.java

/**
 * Method to create finished queries table, this is required for embedded lens server. For production server we will
 * not be creating tables as it would be created upfront.
 *
 * @throws Exception the exception//from   www  .  j a  v  a 2 s.c  om
 */
public void createFinishedQueriesTable() throws Exception {
    String sql = "CREATE TABLE if not exists finished_queries (handle varchar(255) not null unique,"
            + "userquery varchar(20000) not null," + "submitter varchar(255) not null,"
            + "priority varchar(255), " + "starttime bigint, " + "endtime bigint," + "result varchar(255),"
            + "status varchar(255), " + "metadata varchar(100000), " + "rows int, " + "filesize bigint, "
            + "errormessage varchar(10000), " + "driverstarttime bigint, " + "driverendtime bigint, "
            + "drivername varchar(10000), " + "queryname varchar(255), " + "submissiontime bigint, "
            + "driverquery varchar(1000000), " + "conf varchar(100000), numfailedattempts int)";
    try {
        QueryRunner runner = new QueryRunner(ds);
        runner.update(sql);
        log.info("Created finished queries table");
    } catch (SQLException e) {
        log.warn("Unable to create finished queries table", e);
    }
}

From source file:org.apache.lens.server.query.LensServerDAO.java

public void createFailedAttemptsTable() throws Exception {
    String sql = "CREATE TABLE if not exists failed_attempts (handle varchar(255) not null,"
            + "attempt_number int, drivername varchar(10000), progress float, progressmessage varchar(10000), "
            + "errormessage varchar(10000), driverstarttime bigint, driverendtime bigint)";
    try {//from w  w  w  .  ja  v  a 2s  .c om
        QueryRunner runner = new QueryRunner(ds);
        runner.update(sql);
        log.info("Created failed_attempts table");
    } catch (SQLException e) {
        log.error("Unable to create failed_attempts table", e);
    }
}