Example usage for java.sql Statement executeUpdate

List of usage examples for java.sql Statement executeUpdate

Introduction

In this page you can find the example usage for java.sql Statement executeUpdate.

Prototype

int executeUpdate(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

Usage

From source file:com.emergya.persistenceGeo.dbutils.DatabaseUtilsImpl.java

private void changeColumnName(String tableName, String oldName, String newName) throws SQLException {
    Connection con = ds.getConnection();
    Statement stm = con.createStatement();
    String sql = "ALTER TABLE " + tableName + " RENAME COLUMN " + oldName + " TO " + newName;
    stm.executeUpdate(sql);
    stm.close();/*ww  w.  j av a 2 s .  c o m*/
    con.close();

}

From source file:dk.teachus.backend.test.CreateMysqlTestDatabase.java

public void executeUpdateSql(Connection connection, CharSequence sql) throws SQLException {
    List<String> statements = parseSqlIntoSingleStatements(sql);

    for (String sqlStatement : statements) {
        Statement statement = null;

        try {/*from w w  w .ja va2  s.  co m*/
            statement = connection.createStatement();
            statement.executeUpdate(sqlStatement);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

From source file:hu.bme.mit.trainbenchmark.sql.SQLDatabaseDriver.java

@Override
public void insertEdge(final Long sourceVertex, final String sourceVertexType, final Long targetVertex,
        final String edgeType) throws IOException {

    try {/*  ww  w.j  a  v  a 2 s  .  c  o  m*/
        final Statement st = con.createStatement();
        st.executeUpdate(String.format("INSERT INTO `%s` (`%s`, `%s`) VALUES (%d, %d);",
                EDGE_TABLE.get(edgeType), EDGE_SOURCE_TYPES.get(edgeType), EDGE_TARGET_TYPES.get(edgeType),
                sourceVertex, targetVertex));
    } catch (final SQLException e) {
        throw new IOException(e);
    }
}

From source file:com.reydentx.core.client.MySQLClient.java

public int executeUpdate(String query) {
    Connection conn = borrowClient();
    if (conn == null) {
        return -(1);
    }/*from w w w .jav  a  2s.c  o m*/
    try {
        Statement statement = conn.createStatement();
        int ret = statement.executeUpdate(query);
        returnObject(conn);
        return ret;
    } catch (Exception ex) {
        _logger.error(ex.getMessage(), ex);
        invalidClient(conn);
        return -(1);
    }
}

From source file:com.amediamanager.config.DatabaseSchemaResource.java

private void provisionDataSource(final String dropTableQuery, final String createTableQuery) {

    Connection connection = null;
    Statement statement = null;
    try {//from   w w  w .  j  a v a 2s.  c om
        connection = dataSource.getConnection();
        statement = connection.createStatement();

        statement.executeUpdate(dropTableQuery);
        statement.executeUpdate(createTableQuery);

    } catch (SQLException e) {
        LOG.warn("Failed provisioning datasource", e);
    } finally {
        try {
            statement.close();
            connection.close();
        } catch (Exception x) {
        }
    }
}

From source file:com.silverpeas.wysiwyg.dynamicvalue.AbstractBaseDynamicValue.java

/**
 * @throws java.lang.Exception/*from w  w  w .ja  v a2s  . c o m*/
 */
@Before
@Override
public void setUp() throws Exception {
    properties = new Properties();
    properties.load(TestDynamicValueDAO.class.getClassLoader().getResourceAsStream("jdbc.properties"));
    // create the table to execute the test
    Connection con = null;
    Statement statement = null;

    try {
        con = getConnection().getConnection();
        statement = con.createStatement();

        try {
            statement.executeUpdate("DROP TABLE val_dyn1");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String sql = "CREATE TABLE val_dyn1 ( \"value\" character varying(256) NOT NULL,"
                + "keyword character varying(100) NOT NULL, start_date date NOT NULL, end_date date )"
                + "WITH (OIDS=FALSE);" + "ALTER TABLE val_dyn1 OWNER TO " + getUsername();
        statement.executeUpdate(sql);
    } finally {
        DBUtil.close(statement);
        DBUtil.close(con);
    }
    super.setUp();
}

From source file:hu.bme.mit.trainbenchmark.sql.SQLDatabaseDriver.java

@Override
public void deleteVertex(final Long vertex, final String vertexType) throws IOException {
    try {// w  w  w.  jav a 2 s .  c om
        final Statement statement = con.createStatement();
        statement.executeUpdate(String.format("DELETE FROM `%s` WHERE `%s` = %d;", vertexType, ID, vertex));
    } catch (final SQLException e) {
        throw new IOException(e);
    }

}

From source file:com.gdo.project.util.SqlUtils.java

public static void update(StclContext stclContext, Query query) {
    Statement stmt = null;
    try {/*w  w  w  .  jav  a2 s .  c om*/
        stmt = createStatement(stclContext);
        stmt.executeUpdate(query.query());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stmt != null)
            closeStatement(stclContext, stmt);
    }
}

From source file:com.persistent.cloudninja.utils.ProvisionConnectionUtility.java

public int getConnectionDetails(String url, String query, String db_name, String db_user, String db_user_pwd)
        throws Exception {

    Connection connection = null;
    Statement statement = null;
    int update_flag = 0;

    try {/* ww w.j a v  a  2  s. c  om*/

        connection = getConnection(url, db_name, db_user, db_user_pwd);
        statement = connection.createStatement();
        update_flag = statement.executeUpdate(query);
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Problem occured while getting connection details.");
    } finally {
        closeStatement(statement);
        closeConnection(connection);
    }
    return update_flag;
}

From source file:com.taobao.tddl.jdbc.group.integration.CRUDTest.java

    () throws Exception {
   TGroupDataSource ds = new TGroupDataSource();
   DataSourceWrapper dsw = new DataSourceWrapper("db1", "rw", DataSourceFactory.getMySQLDataSource(), DBType.MYSQL);
   ds.init(dsw);//from w  ww.j ava  2  s  .  co  m
      
   Connection conn = ds.getConnection();

   //Statementcrud
   Statement stmt = conn.createStatement();
   assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
   assertEquals(stmt.executeUpdate("update crud set f2='str2'"), 1);
   ResultSet rs = stmt.executeQuery("select f1,f2 from crud");
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   assertEquals(stmt.executeUpdate("delete from crud"), 1);
   rs.close();
   stmt.close();

   //PreparedStatementcrud
   String sql = "insert into crud(f1,f2) values(?,?)";
   PreparedStatement ps = conn.prepareStatement(sql);
   ps.setInt(1, 10);
   ps.setString(2, "str");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "update crud set f2=?";
   ps = conn.prepareStatement(sql);
   ps.setString(1, "str2");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "select f1,f2 from crud";
   ps = conn.prepareStatement(sql);
   rs = ps.executeQuery();
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   rs.close();
   ps.close();

   sql = "delete from crud";
   ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   conn.close();
}