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:bizlogic.Sensors.java

public static void add(Connection DBcon, String name, String IP, String operation, String operand, String unit,
        String port) throws SQLException {

    String _operation = operation;
    Statement st = null;
    ResultSet rs = null;//  w  ww.  j  ava 2 s  . c  o m

    switch (operation) {
    case "Add":
        _operation = "+";
        break;
    case "Subtract":
        _operation = "-";
        break;
    case "Multiply":
        _operation = "*";
        break;
    case "Divide":
        _operation = "/";
        break;
    default:
        break;

    }

    String sql_statement;

    st = DBcon.createStatement();
    //for(int i = 0; i<30; i++) {
    sql_statement = "INSERT INTO USERCONF.SENSORLIST(OPERATION, OPERAND, NAME, \"IP\", UNIT, PORT) "
            + "VALUES (" + "'" + _operation + "'" + ", " + "'" + operand + "'" + ", " + "'" + name + "'" + ", "
            + "'" + IP + "'" + ", " + "'" + unit + "'" + ", " + "'" + port + "'" + " );";
    System.out.println(sql_statement);
    st.clearBatch();
    st = DBcon.createStatement();
    DBcon.createStatement();
    st.executeUpdate(sql_statement);

}

From source file:car_counter.storage.sqlite.SqliteStorage.java

private void initialiseTables() throws SQLException {
    Collection<String> tables = new ArrayList<String>();
    ResultSet resultSet = connection.getMetaData().getTables(null, null, "%", null);
    while (resultSet.next()) {
        tables.add(resultSet.getString(3));
    }/*from   w w  w .j  a  v a  2  s . c o  m*/

    if (tables.contains("detected_vehicles")) {
        return;
    }

    Statement statement = connection.createStatement();

    statement.executeUpdate("create table detected_vehicles (" + "id integer primary key,"
            + "timestamp integer," + "initial_location integer," + "end_location integer," + "speed float,"
            + "colour string," + "video_file string)");
}

From source file:org.web4thejob.orm.CreateSchemaTest.java

@Test
public void schemaExportTest() throws IOException, SQLException {

    Log4jConfigurer.initLogging("classpath:org/web4thejob/conf/log4j.xml");

    Properties datasource = new Properties();
    datasource.load(new ClassPathResource(DatasourceProperties.PATH).getInputStream());

    final Configuration configuration = new Configuration();
    configuration.setProperty(AvailableSettings.DIALECT, datasource.getProperty(DatasourceProperties.DIALECT));
    configuration.setProperty(AvailableSettings.DRIVER, datasource.getProperty(DatasourceProperties.DRIVER));
    configuration.setProperty(AvailableSettings.URL, "jdbc:hsqldb:mem:mydb");
    configuration.setProperty(AvailableSettings.USER, datasource.getProperty(DatasourceProperties.USER));
    configuration.setProperty(AvailableSettings.PASS, datasource.getProperty(DatasourceProperties.PASSWORD));

    final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();

    Connection connection = serviceRegistry.getService(ConnectionProvider.class).getConnection();
    Statement statement = connection.createStatement();
    statement.executeUpdate("CREATE SCHEMA w4tj;");
    statement.close();//from  w ww. ja  v  a 2 s. c  o m

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
        for (Resource resource : resolver.getResources("classpath*:org/web4thejob/orm/**/*.hbm.xml")) {

            if (resource.getFile().getName().equals("AuxiliaryDatabaseObjects.hbm.xml"))
                continue;

            configuration.addFile(resource.getFile());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    SchemaExport schemaExport = new SchemaExport(serviceRegistry, configuration);
    schemaExport.execute(Target.EXPORT, SchemaExport.Type.CREATE);

    if (!schemaExport.getExceptions().isEmpty()) {
        throw new RuntimeException((Throwable) schemaExport.getExceptions().get(0));
    }

}

From source file:com.mirth.connect.donkey.server.data.jdbc.SqlServerDaoFactory.java

@Override
public JdbcDao getDao(SerializerProvider serializerProvider) {
    JdbcDao dao = super.getDao(serializerProvider);

    if (deadlockPriority != null) {
        Statement statement = null;

        try {//  w w  w  .  j a  v  a  2  s.c  o m
            statement = dao.getConnection().createStatement();
            statement.executeUpdate("SET DEADLOCK_PRIORITY " + deadlockPriority);
        } catch (SQLException e) {
            throw new DonkeyDaoException(e);
        } finally {
            DbUtils.closeQuietly(statement);
        }
    }

    return dao;
}

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

@Test
public void springTest() throws Exception {

    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();//w  ww .  ja  v a  2 s .c o  m
    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(10,'str')";
    PreparedStatement ps = conn.prepareStatement(sql);
    assertEquals(ps.executeUpdate(), 1);
    ps.close();

    sql = "update crud set f2='str2'";
    ps = conn.prepareStatement(sql);
    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();
}

From source file:kr.co.bitnine.octopus.testutils.MemoryDatabase.java

public void runExecuteUpdate(String sqlStmt) throws SQLException {
    Statement stmt = initialConnection.createStatement();
    stmt.executeUpdate(sqlStmt);
    stmt.close();/*from www. j av a  2 s.  co m*/
}

From source file:org.brixcms.rmiserver.boot.Bootstrapper.java

/**
 * @param sf/*from   w ww . ja  va  2s.c om*/
 */
private void bootstrapSchema() {
    Connection con = null;
    try {
        con = datasource.getConnection();
        Dialect dialect = Dialect.getDialect(configuration.getProperties());
        String[] schema = configuration.generateSchemaCreationScript(dialect);
        for (String stmt : schema) {
            Statement st = con.createStatement();
            st.executeUpdate(stmt);
            st.close();
        }
        con.commit();
    } catch (SQLException e1) {
        if (con != null) {
            try {
                con.rollback();
            } catch (SQLException e2) {
                try {
                    con.close();
                } catch (SQLException e3) {
                }
            }
        }
    }
}

From source file:com.nilesh.GenericResourse.java

@POST
@Path("/products")
@Consumes(MediaType.APPLICATION_JSON)/* w  w w .j a  v  a  2  s  . c  om*/
@Produces(MediaType.APPLICATION_JSON)

public Response createProduct(String str)
        throws SQLException, ParseException, org.json.simple.parser.ParseException {

    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(str);

    Object id = json.get("id");
    String productid = id.toString();
    int Id = Integer.parseInt(productid);
    Object name = json.get("name");
    String productname = name.toString();
    Object description = json.get("description");
    String productdescription = description.toString();
    Object quantity = json.get("quantity");
    String productquantity = quantity.toString();
    int Qnt = Integer.parseInt(productquantity);
    Statement smt = conn.createStatement();
    smt.executeUpdate("INSERT INTO product VALUES ('" + Id + "','" + productname + "','" + productdescription
            + "','" + Qnt + "' )");
    return Response.status(Response.Status.CREATED).build();
}

From source file:com.Assignment4.Products.java

/**
 * converting integer into string and parsing it to jsonobject
 * @param s returns the query in the and executes the insert query
 * @throws ParseException/*w  ww. ja  v  a  2  s  .c  om*/
 * @throws SQLException
 */
@POST
@Path("/product")
@Consumes(MediaType.APPLICATION_JSON)
public void createProducts(String str) throws ParseException, SQLException {

    JSONParser jparser = new JSONParser();
    JsonObject jobject = (JsonObject) jparser.parse(str);

    Object ID = jobject.get("id");
    String ProductID = ID.toString();
    int iD = Integer.parseInt(ProductID);

    Object Name = jobject.get("name");
    String name = Name.toString();

    Object Description = jobject.get("description");
    String description = Description.toString();

    Object Qty = jobject.get("quantity");
    String quantity = Qty.toString();
    int Quantity = Integer.parseInt(quantity);

    Connection conn = getConnection();
    Statement s = conn.createStatement();
    s.executeUpdate("INSERT INTO product VALUES ('" + iD + "','" + name + "','" + description + "','" + quantity
            + "' )");
}

From source file:com.l2jfree.sql.L2DataSourceSQLite.java

@Override
public void optimize() throws SQLException {
    Connection con = null;/*from w w  w. ja va 2s.co  m*/
    try {
        con = getConnection();

        final Statement st = con.createStatement();

        _log.info("TableOptimizer: Rebuilding database...");
        st.executeUpdate("VACUUM");
        _log.info("TableOptimizer: Building usage statistics...");
        st.executeUpdate("ANALYZE");

        _log.info("TableOptimizer: Database tables have been optimized.");

        st.close();
    } finally {
        L2Database.close(con);
    }
}