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, Object... params) throws SQLException 

Source Link

Document

Executes the given INSERT, UPDATE, or DELETE SQL statement.

Usage

From source file:cn.itcast.bbs.dao.UserDao.java

public void saveUser(User user) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "insert into user(username,password,gender,email) values(?,?,?,?);";
    runner.update(sql,
            new Object[] { user.getUsername(), user.getPassword(), user.getGender(), user.getEmail() });
}

From source file:cn.itcast.bbs.dao.TypeDao.java

public void updateTypeClick(int id) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "update type set click = click +1 where id = ?;";
    runner.update(sql, id);
}

From source file:io.dockstore.common.BasicPostgreSQL.java

protected boolean runUpdateStatement(String query, Object... params) {
    try {/*from   www  .java 2 s  . c o m*/
        QueryRunner run = new QueryRunner(dataSource);
        run.update(query, params);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:com.fluke.database.dataservice.EODDao.java

public int insertEODTicker(String equity, EODTicker ticker) throws SQLException {
    String sql = "insert into EOD (equity,openPrice,closePrice,highPrice,lowPrice,adjustedClose,volume,date) values(?,?,?,?,?,?,?,?)";
    String date = Util.getDate(ticker.getDate());
    Object[] params = new Object[] { equity, ticker.getOpenPrice(), ticker.getClosePrice(),
            ticker.getHighPrice(), ticker.getLowPrice(), ticker.getAdjustedClose(), ticker.getVolume(), date };
    QueryRunner run = new QueryRunner(DatabaseProperty.getDataSource());
    int updates = run.update(sql, params);
    //run.update("delete from EOD where volume=0");
    return updates;
}

From source file:ch.vorburger.mariadb4j.tests.MariaDB4jSampleTutorialTest.java

@Test
public void testEmbeddedMariaDB4j() throws Exception {
    DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
    config.setPort(0); // 0 => autom. detect free port
    DB db = DB.newEmbeddedDB(config.build());
    db.start();/*  ww w.j a  v a2s  . c o m*/

    String dbName = "mariaDB4jTest"; // or just "test"
    if (!dbName.equals("test")) {
        // mysqld out-of-the-box already has a DB named "test"
        // in case we need another DB, here's how to create it first
        db.createDB(dbName);
    }

    Connection conn = null;
    try {
        conn = DriverManager.getConnection(config.getURL(dbName), "root", "");
        QueryRunner qr = new QueryRunner();

        // Should be able to create a new table
        qr.update(conn, "CREATE TABLE hello(world VARCHAR(100))");

        // Should be able to insert into a table
        qr.update(conn, "INSERT INTO hello VALUES ('Hello, world')");

        // Should be able to select from a table
        List<String> results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>());
        Assert.assertEquals(1, results.size());
        Assert.assertEquals("Hello, world", results.get(0));

        // Should be able to source a SQL file
        db.source("ch/vorburger/mariadb4j/testSourceFile.sql", "root", null, dbName);
        results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>());
        Assert.assertEquals(3, results.size());
        Assert.assertEquals("Hello, world", results.get(0));
        Assert.assertEquals("Bonjour, monde", results.get(1));
        Assert.assertEquals("Hola, mundo", results.get(2));
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:cn.itcast.bbs.dao.ReplyDao.java

public void addReply(Reply reply, int topicId) throws Exception {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "insert into reply(title,name,content,topic_id) values(?,?,?,?);";
    runner.update(sql, new Object[] { reply.getTitle(), reply.getName(), reply.getContent(), topicId });
}

From source file:cn.itcast.bbs.dao.TopicDao.java

public void updateTopic(Topic topic) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "update topic set title=?,content=? where id=?;";
    runner.update(sql, new Object[] { topic.getTitle(), topic.getContent(), topic.getId() });
}

From source file:cn.itcast.bbs.dao.TopicDao.java

public void addTopic(Topic topic, int id) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "insert into topic(title,name,content,type_id) values(?,?,?,?);";
    runner.update(sql, new Object[] { topic.getTitle(), topic.getName(), topic.getContent(), id });
}

From source file:azkaban.db.DatabaseSetup.java

private void runTableScripts(final Connection conn, final String table) throws IOException, SQLException {
    logger.info("Creating new table " + table);

    final String dbSpecificScript = "create." + table + ".sql";
    final File script = new File(this.scriptPath, dbSpecificScript);
    BufferedInputStream buff = null;
    try {/*from w  w  w . ja  v a2  s .co  m*/
        buff = new BufferedInputStream(new FileInputStream(script));
        final String queryStr = IOUtils.toString(buff);
        final String[] splitQuery = queryStr.split(";\\s*\n");
        final QueryRunner runner = new QueryRunner();
        for (final String query : splitQuery) {
            runner.update(conn, query);
        }
        conn.commit();
    } finally {
        IOUtils.closeQuietly(buff);
    }
}

From source file:de.unibremen.informatik.tdki.combo.rewriting.FakeFilterRewriter.java

private void createFilterInDB2() {
    for (int j = 1; j < 6; j++) {
        StringBuilder builder = new StringBuilder();
        builder.append("CREATE FUNCTION ");

        builder.append("fake_filter").append(libNo).append("(");
        for (int i = 0; i < j; i++) {
            builder.append("inParm").append(i).append(" INTEGER");
            if (i < j - 1) {
                builder.append(", ");
            }//  w  ww  .jav  a  2s . c o m
        }
        builder.append(")\n");
        builder.append("RETURNS INTEGER\n");
        builder.append("LANGUAGE C\n");
        builder.append("PARAMETER STYLE SQL\n");
        builder.append("NO SQL\n");
        builder.append("NOT FENCED\n");
        builder.append("THREADSAFE\n");
        builder.append("DETERMINISTIC\n");
        builder.append("RETURNS NULL ON NULL INPUT\n");
        builder.append("NO EXTERNAL ACTION\n");
        builder.append("EXTERNAL NAME \'").append(outputDir).append("libDB2FakeFilter").append(libNo)
                .append(".dylib!fake_filter").append(libNo).append("\'");
        QueryRunner qRunner = new QueryRunner();
        try {
            qRunner.update(connection, builder.toString());
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
}