List of usage examples for org.springframework.jdbc.core JdbcTemplate execute
@Override public void execute(final String sql) throws DataAccessException
From source file:com.springdeveloper.hadoop.hive.HiveJdbcApp.java
public static void main(String[] args) throws Exception { AbstractApplicationContext context = new ClassPathXmlApplicationContext( "/META-INF/spring/hive-jdbc-context.xml", HiveJdbcApp.class); log.info("Hive JDBC Application Running"); context.registerShutdownHook();//from www .j a v a 2 s . c o m String tableDdl = "create external table if not exists tweetdata (value STRING) LOCATION '/tweets/input'"; String query = "select r.retweetedUser, '\t', count(r.retweetedUser) as count " + " from tweetdata j " + " lateral view json_tuple(j.value, 'retweet', 'retweetedStatus') t as retweet, retweetedStatus " + " lateral view json_tuple(t.retweetedStatus, 'fromUser') r as retweetedUser " + " where t.retweet = 'true' " + " group by r.retweetedUser order by count desc limit 10"; String results = "insert overwrite directory '/tweets/hiveout'"; JdbcTemplate template = context.getBean(JdbcTemplate.class); template.execute(tableDdl); template.execute(results + " " + query); context.close(); }
From source file:com.kaidad.utilities.MBTilesBase64Converter.java
private static void renameTileData(JdbcTemplate c) { c.execute("DROP TABLE images"); c.execute("ALTER TABLE images_string RENAME TO images"); System.out.println("Successfully moved table 'images_string' to 'images'"); }
From source file:com.wlami.mibox.client.db.DbBootstraper.java
public static void bootstrap(DataSource dataSource) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); try {//w w w . j a v a 2 s.c o m jdbcTemplate.execute("SELECT 1 FROM meta_sync"); } catch (DataAccessException e) { jdbcTemplate.execute(CREATE_TABLE_META_SYNC); } }
From source file:com.kaidad.utilities.MBTilesBase64Converter.java
private static void createTempTable(JdbcTemplate c) { Integer i = c.queryForObject( "select count(*) from sqlite_master WHERE type='table' AND name='images_string'", Integer.class); if (i == 0) { c.execute("create table images_string (tile_data TEXT, tile_id TEXT)"); }/* ww w .ja v a 2 s . co m*/ System.out.println("Successfully created table images_string 'tile_data_string'"); }
From source file:com.jagornet.dhcp.db.DbSchemaManager.java
/** * Creates the schema.//from ww w. j ava2 s.c o m * * @param dataSource the data source * * @throws SQLException if there is a problem with the database * @throws IOExcpetion if there is a problem reading the schema file */ public static void createSchema(DataSource dataSource, String schemaFilename) throws SQLException, IOException { log.info("Creating new JDBC schema from file: " + schemaFilename); JdbcTemplate jdbc = new JdbcTemplate(dataSource); List<String> schemaDDL = getSchemaDDL(schemaFilename); for (String ddl : schemaDDL) { jdbc.execute(ddl); } }
From source file:no.trank.openpipe.jdbc.SimpleJdbcDocumentProducer.java
private static void execute(JdbcTemplate jdbcTemplate, List<String> sqls) throws DataAccessException { for (String sql : sqls) { log.debug("Executing sql {}", sql); jdbcTemplate.execute(sql); }//from w ww . jav a2 s . c om }
From source file:be.jacobsvanroy.springsqlunit.util.ScriptUtils.java
/** * Execute the given SQL script./* w w w.ja v a 2s.c o m*/ * <p>Statement separators and comments will be removed before executing * individual statements within the supplied script. * <p><b>Do not use this method to execute DDL if you expect rollback.</b> * * @param dataSource the dataSource to use to execute the script; already * configured and ready to use * @param resource the resource (potentially associated with a specific encoding) * to load the SQL script from * @param commentPrefix the prefix that identifies comments in the SQL script — * typically "--" * @param separator the script statement separator; defaults to * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a * single statement without a separator * @param blockCommentStartDelimiter the <em>start</em> block comment delimiter; never * {@code null} or empty * @param blockCommentEndDelimiter the <em>end</em> block comment delimiter; never * {@code null} or empty * @see #DEFAULT_STATEMENT_SEPARATOR * @see #FALLBACK_STATEMENT_SEPARATOR * @see #EOF_STATEMENT_SEPARATOR */ public static void executeSqlScript(DataSource dataSource, EncodedResource resource, String commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) { try { if (logger.isInfoEnabled()) { logger.info("Executing SQL script from " + resource); } long startTime = System.currentTimeMillis(); List<String> statements = new LinkedList<String>(); String script; try { script = readScript(resource, commentPrefix, separator); } catch (IOException ex) { throw new CannotReadScriptException(resource, ex); } if (separator == null) { separator = DEFAULT_STATEMENT_SEPARATOR; } if (!EOF_STATEMENT_SEPARATOR.equals(separator) && !containsSqlScriptDelimiters(script, separator)) { separator = FALLBACK_STATEMENT_SEPARATOR; } splitSqlScript(resource, script, separator, commentPrefix, blockCommentStartDelimiter, blockCommentEndDelimiter, statements); for (String statement : statements) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.execute(statement); } long elapsedTime = System.currentTimeMillis() - startTime; if (logger.isInfoEnabled()) { logger.info("Executed SQL script from " + resource + " in " + elapsedTime + " ms."); } } catch (Exception ex) { throw new RuntimeException("Failed to execute database script from resource [" + resource + "]", ex); } }
From source file:net.firejack.platform.core.utils.db.DBUtils.java
/** * @param dataSource// w w w. j a v a 2 s. c o m * @param sql * @return */ public static boolean executeStatement(DataSource dataSource, String sql) { JdbcTemplate template = new JdbcTemplate(dataSource); try { template.execute(sql); } catch (DataAccessException e) { logger.error(e.getMessage(), e); return false; } return true; }
From source file:com.alibaba.otter.shared.common.utils.meta.DdlUtils.java
public static Table findTable(final JdbcTemplate jdbcTemplate, final String catalogName, final String schemaName, final String tableName, final DdlUtilsFilter filter) throws Exception { return (Table) jdbcTemplate.execute(new ConnectionCallback() { public Object doInConnection(Connection con) throws SQLException, DataAccessException { Table table = null;//from w w w . ja v a 2 s . c om DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper(); try { if (filter != null) { con = filter.filterConnection(con); Assert.notNull(con); } DatabaseMetaData databaseMetaData = con.getMetaData(); if (filter != null) { databaseMetaData = filter.filterDataBaseMetaData(jdbcTemplate, con, databaseMetaData); Assert.notNull(databaseMetaData); } metaData.setMetaData(databaseMetaData); metaData.setTableTypes(TableType.toStrings(SUPPORTED_TABLE_TYPES)); metaData.setCatalog(catalogName); metaData.setSchemaPattern(schemaName); String convertTableName = tableName; if (databaseMetaData.storesUpperCaseIdentifiers()) { metaData.setCatalog(catalogName.toUpperCase()); metaData.setSchemaPattern(schemaName.toUpperCase()); convertTableName = tableName.toUpperCase(); } if (databaseMetaData.storesLowerCaseIdentifiers()) { metaData.setCatalog(catalogName.toLowerCase()); metaData.setSchemaPattern(schemaName.toLowerCase()); convertTableName = tableName.toLowerCase(); } ResultSet tableData = null; try { tableData = metaData.getTables(convertTableName); while ((tableData != null) && tableData.next()) { Map<String, Object> values = readColumns(tableData, initColumnsForTable()); table = readTable(metaData, values); if (table.getName().equalsIgnoreCase(tableName)) { break; } } } finally { JdbcUtils.closeResultSet(tableData); } } catch (Exception e) { logger.error(e.getMessage(), e); } makeAllColumnsPrimaryKeysIfNoPrimaryKeysFound(table); return table; } }); }
From source file:com.alibaba.otter.shared.common.utils.meta.DdlUtils.java
@SuppressWarnings("unchecked") public static List<Table> findTables(final JdbcTemplate jdbcTemplate, final String catalogName, final String schemaName, final String tableNamePattern, final DdlUtilsFilter filter, final DdlTableNameFilter tableNameFilter) throws Exception { return (List<Table>) jdbcTemplate.execute(new ConnectionCallback() { public Object doInConnection(Connection con) throws SQLException, DataAccessException { List<Table> tables = new ArrayList<Table>(); DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper(); try { if (filter != null) { con = filter.filterConnection(con); Assert.notNull(con); }/* ww w. j a v a2s . c o m*/ DatabaseMetaData databaseMetaData = con.getMetaData(); if (filter != null) { databaseMetaData = filter.filterDataBaseMetaData(jdbcTemplate, con, databaseMetaData); Assert.notNull(databaseMetaData); } metaData.setMetaData(databaseMetaData); metaData.setTableTypes(TableType.toStrings(SUPPORTED_TABLE_TYPES)); metaData.setCatalog(catalogName); metaData.setSchemaPattern(schemaName); String convertTableName = tableNamePattern; if (databaseMetaData.storesUpperCaseIdentifiers()) { metaData.setCatalog(catalogName.toUpperCase()); metaData.setSchemaPattern(schemaName.toUpperCase()); convertTableName = tableNamePattern.toUpperCase(); } if (databaseMetaData.storesLowerCaseIdentifiers()) { metaData.setCatalog(catalogName.toLowerCase()); metaData.setSchemaPattern(schemaName.toLowerCase()); convertTableName = tableNamePattern.toLowerCase(); } ResultSet tableData = null; try { tableData = metaData.getTables(convertTableName); while ((tableData != null) && tableData.next()) { Map<String, Object> values = readColumns(tableData, initColumnsForTable()); Table table = readTable(metaData, values); if ((tableNameFilter == null) || tableNameFilter.accept(catalogName, schemaName, table.getName())) { tables.add(table); } } } finally { JdbcUtils.closeResultSet(tableData); } } catch (Exception e) { logger.error(e.getMessage(), e); } for (Table table : tables) { makeAllColumnsPrimaryKeysIfNoPrimaryKeysFound(table); } return tables; } }); }