List of usage examples for java.sql Statement executeUpdate
int executeUpdate(String sql) throws SQLException;
INSERT
, UPDATE
, or DELETE
statement or an SQL statement that returns nothing, such as an SQL DDL statement. From source file:info.jtrac.config.DataSourceFactoryBean.java
/** * This method is called to clean up the references when the object is * destroyed./*from w ww .j a v a2s . c o m*/ * * @throws Exception * @see org.springframework.beans.factory.DisposableBean#destroy() */ @Override public void destroy() throws Exception { if (dataSource instanceof SingleConnectionDataSource) { logger.info("attempting to shut down embedded HSQLDB database"); Connection con = dataSource.getConnection(); Statement stmt = con.createStatement(); stmt.executeUpdate("SHUTDOWN"); stmt.close(); con.close(); logger.info("embedded HSQLDB database shut down successfully"); } else if (dataSource instanceof BasicDataSource) { logger.info("attempting to close Apache DBCP data source"); ((BasicDataSource) dataSource).close(); logger.info("Apache DBCP data source closed successfully"); } else { logger.info("context shutting down for JNDI datasource"); } // end if..else }
From source file:com.mirth.connect.server.migration.Migrator.java
protected int executeStatement(String statementString) throws SQLException { Statement statement = null; try {/*from w ww . java 2 s . c o m*/ statement = getConnection().createStatement(); return statement.executeUpdate(statementString); } finally { DbUtils.close(statement); } }
From source file:eu.optimis.aggregator.test.AggregatorPushTest.java
private boolean deleteResources() { DBConnection dbconn = new DBConnection(); Connection conn = dbconn.getConnection(); String queryPhysical = null;/*from w w w . j a v a 2 s . co m*/ String queryVirtual = null; queryPhysical = "Delete FROM monitoring_resource_physical where physical_resource_id like '" + "%UnitTest%" + "'"; queryVirtual = "Delete FROM monitoring_resource_virtual where virtual_resource_id like '" + "%UnitTest%" + "'"; try { Statement st = conn.createStatement(); st.executeUpdate(queryPhysical); st.executeUpdate(queryVirtual); } catch (SQLException e) { logger.error("SQLException:" + e.getMessage() + ":" + e.getSQLState()); return false; } finally { try { conn.close(); } catch (Exception e) { } ; } return true; }
From source file:dk.teachus.backend.test.CreateMysqlTestDatabase.java
public CreateMysqlTestDatabase(Resource property) { System.out.print("Ensuring database exists... "); // Create connection Connection connection = null; try {// w ww .j ava2 s .c o m Properties properties = new Properties(); properties.load(property.getInputStream()); String driverClass = properties.getProperty("db.driverClassName"); String jdbcUrl = properties.getProperty("db.url"); jdbcUrl += "?allowMultiQueries=true"; String jdbcUser = properties.getProperty("db.username"); String jdbcPass = properties.getProperty("db.password"); String jdbcHost = properties.getProperty("db.host"); String jdbcDatabase = properties.getProperty("db.database"); Class.forName(driverClass); // Create database if not exists connection = DriverManager.getConnection("jdbc:mysql://" + jdbcHost + "/" + jdbcDatabase, jdbcUser, jdbcPass); Statement statement = connection.createStatement(); statement.executeUpdate("DROP DATABASE " + jdbcDatabase); statement.close(); statement = connection.createStatement(); statement.executeUpdate("CREATE DATABASE " + jdbcDatabase); statement.close(); connection.close(); // Connect to database connection = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass); // Drop existing tables dropTable(connection, "booking"); dropTable(connection, "message_recipient"); dropTable(connection, "message"); dropTable(connection, "application_configuration"); dropTable(connection, "period"); dropTable(connection, "teacher_attribute"); // To drop the person table we first need to remove all person references try { executeUpdateSql(connection, "UPDATE person SET teacher_id = NULL"); } catch (SQLException e) { // The person table might not exist, so it's ok } dropTable(connection, "person"); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } System.out.println("Done"); } }
From source file:ActualizadorLocal.Clientes.ClienteNodos.java
public void InsertarDatosSync(String datos) throws SQLException { try {//w w w . jav a2 s .c o m Statement st = conexion.crearSt(); st.executeUpdate("INSERT INTO nodo (idNodo, nombre, latitud, longitud,poligono) VALUES (" + datos + " ) ON DUPLICATE KEY UPDATE nombre=nombre, latitud=latitud, longitud=longitud, poligono=poligono"); } catch (com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException e) { return; } }
From source file:org.trustedanalytics.datasetpublisher.service.HiveServiceTest.java
@Test(expected = RuntimeException.class) public void testCreateTable_executionQueryException_propagatesAsRuntime() throws Exception { //given/*www.java 2 s .c o m*/ final HiveTable hiveTable = new HiveTable("db", "table", Collections.emptyList(), "loc"); Connection connection = mock(Connection.class); Statement stm = mock(Statement.class); when(connection.createStatement()).thenReturn(stm); when(hiveClient.getConnection(userIdentity)).thenReturn(connection); when(stm.executeUpdate(anyObject())).thenThrow(LoginException.class); //when hiveService.createTable(hiveTable, userIdentity); //then //any Exception in executing query propagates as RuntimeException }
From source file:com.zionex.t3sinc.util.db.SincDatabaseUtility.java
public int executeUpdate(Statement statement, String sqlString) throws SQLException { return statement.executeUpdate(sqlString); }
From source file:ActualizadorLocal.Clientes.ClienteNodos.java
public void syncDB() { try {//from w ww . ja v a 2 s. co m Statement st = conexion.crearSt(); st.executeUpdate("INSERT INTO nodo (idNodo, nombre, latitud, longitud, poligono) VALUES" + cache + "ON DUPLICATE KEY UPDATE nombre=VALUES(nombre), latitud=VALUES(latitud), longitud=VALUES(longitud), poligono=VALUES(poligono); "); } catch (SQLException ex) { System.err.println(ex.getMessage()); } cache_size = 0; cache = ""; }
From source file:com.taobao.tddl.jdbc.group.integration.CRUDTest.java
() throws Exception { // DataSource ds1 = DataSourceFactory.getMySQLDataSource(1); DataSource ds2 = DataSourceFactory.getMySQLDataSource(2); DataSource ds3 = DataSourceFactory.getMySQLDataSource(3); //from w ww.j a v a 2 s. c om //db3db2db1 TGroupDataSource ds = new TGroupDataSource(); DataSourceWrapper dsw1 = new DataSourceWrapper("db1", "w", ds1, DBType.MYSQL); DataSourceWrapper dsw2 = new DataSourceWrapper("db2", "r20", ds2, DBType.MYSQL); DataSourceWrapper dsw3 = new DataSourceWrapper("db3", "r30", ds3, DBType.MYSQL); ds.init(dsw1, dsw2, dsw3); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(100,'str')"), 1); // // //db2,db3rs.next()false ResultSet rs = stmt.executeQuery("select f1,f2 from crud where f1=100"); assertFalse(rs.next()); rs.close(); assertEquals(stmt.executeUpdate("delete from crud where f1=100"), 1); stmt.close(); conn.close(); }
From source file:org.netxilia.spi.impl.storage.db.ddl.DDLWriter.java
/** Execute the given DDL queries */ protected void executeDDLQueries(List<String> queryList) throws SQLException { if (queryList.size() == 0) { return;//from w w w. j ava 2 s. c o m } if (logger.isInfoEnabled()) { StringBuilder sb = new StringBuilder("DDLQueries ["); sb.append(queryList.size()).append("]"); for (String query : queryList) { sb.append("\n => ").append(query); } logger.info(sb.toString()); } Connection conn = dataSource.getConnection(); try { for (String query : queryList) { Statement stmt = conn.createStatement(); stmt.executeUpdate(query); } } finally { conn.close(); } }