List of usage examples for java.sql DatabaseMetaData getTables
ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[])
throws SQLException;
From source file:de.static_interface.reallifeplugin.database.AbstractTable.java
public boolean exists() { try {//from ww w . j a va 2 s . com DatabaseMetaData dbm = db.getConnection().getMetaData(); ResultSet tables = dbm.getTables(null, null, name, null); return tables.next(); } catch (Exception e) { return false; } }
From source file:com.unito.repository.JDBCDbInit.java
private boolean existsTable(String sTablename) { ResultSet rs;/*from w w w. j av a2 s .c o m*/ try { DatabaseMetaData dbmd = jdbcTemplate.getDataSource().getConnection().getMetaData(); rs = dbmd.getTables(null, null, sTablename.toUpperCase(), null); if (rs.next()) { LOG.info("Table " + sTablename + " already exists !!"); return true; } else { LOG.info(": table doesn't exist: " + sTablename); return false; } } catch (SQLException e) { LOG.severe(Thread.currentThread().getStackTrace()[1].getMethodName()); } return true; }
From source file:com.uit.anonymousidentity.Repository.Nonces.NonceJDBCTemplate.java
@Override public void createTableIfNotExist() throws SQLException { DatabaseMetaData metaData = dataSource.getConnection().getMetaData(); ResultSet rs = metaData.getTables(null, null, TABLE_NAME, null); if (rs.next()) { //Table exists return;/*from www .j av a2 s. c o m*/ } else { String tem_sql = "create table if not exists %s ( " + "%s int not null auto_increment," + "%s blob not null," + "%s text," + "primary key ( %s ) " + " ) "; String sql = String.format(tem_sql, TABLE_NAME, ID, VALUE, SID, ID); jdbcTemplate.execute(sql); } }
From source file:savant.sql.Database.java
public synchronized List<Table> getTables() throws SQLException { if (tables == null) { tables = new ArrayList<Table>(); DatabaseMetaData md = getConnection().getMetaData(); ResultSet rs = md.getTables(null, null, "%", new String[] { "TABLE" }); int numTables = 0; while (rs.next()) { String s = rs.getString("TABLE_NAME"); tables.add(new Table(s, this)); numTables++;/* w w w. j av a 2 s. c o m*/ LOG.debug(numTables + ": " + s); } LOG.info("Retrieved " + numTables + " tables for " + name); } return tables; }
From source file:com.lotaris.maven.plugin.dbunit.OperationMojo.java
@Override @SuppressWarnings("unchecked") public void execute() throws MojoExecutionException, MojoFailureException { if (skip) {/*from w w w . j a va 2s. c om*/ this.getLog().info("Skip operation: " + type + " execution"); return; } super.execute(); List concatenatedSources = new ArrayList(); CollectionUtils.addIgnoreNull(concatenatedSources, src); if (sources != null) { concatenatedSources.addAll(Arrays.asList(sources)); } try { IDatabaseConnection connection = createConnection(); // Force the database table to be empty before importing data if (clearAllTables) { Connection con = connection.getConnection(); DatabaseMetaData meta = con.getMetaData(); try (ResultSet rs = meta.getTables(null, null, "%", new String[] { "TABLE" })) { while (rs.next()) { String tName = rs.getString("TABLE_NAME"); // Truncate the data con.createStatement().execute("TRUNCATE " + tName + ";"); } } } try { for (Iterator i = concatenatedSources.iterator(); i.hasNext();) { File source = (File) i.next(); Operation op = new Operation(); op.setFormat(format); op.setSrc(source); op.setTransaction(transaction); op.setType(type); op.execute(connection); } } finally { connection.close(); } } catch (Exception e) { throw new MojoExecutionException("Error executing database operation: " + type, e); } }
From source file:com.google.api.ads.adwords.awalerting.sampleimpl.action.SqlDbPersister.java
/** * Initialization action: prepare database table and statement. *//* ww w . j a v a 2 s . co m*/ @Override public void initializeAction() throws AlertProcessingException { batchedInsertions = 0; insertionsCount = 0; try { // Check if table already exists, if not create it. DatabaseMetaData metaData = jdbcTemplate.getDataSource().getConnection().getMetaData(); ResultSet result = metaData.getTables(null, DB_SCHEMA_NAME, DB_TABLE_NAME, null); if (!result.next()) { LOGGER.info("Creating the table {}.{} in database.", DB_SCHEMA_NAME, DB_TABLE_NAME); jdbcTemplate.execute(CREATE_TABLE_SQL); } } catch (SQLException e) { throw new AlertProcessingException("Error invoking SQLDBPersister.initializeAction().", e); } }
From source file:dragonrental.backend.DbConfig.java
public DataSource dataSource() { Properties conf = new Properties(); BasicDataSource ds = new BasicDataSource(); try {/*from w w w. j a v a2 s . co m*/ conf.load(DbConfig.class.getResourceAsStream("/Properties.properties")); } catch (IOException ex) { //log.error("Loading properties has failed!"); } ds.setUrl(conf.getProperty("db.url")); ds.setDriverClassName(conf.getProperty("db.driver")); ds.setUsername(conf.getProperty("db.user")); ds.setPassword(conf.getProperty("db.password")); DatabaseMetaData metaData; ResultSet tables; try (Connection connection = ds.getConnection()) { metaData = connection.getMetaData(); tables = metaData.getTables(null, null, "%", new String[] { "TABLE" }); //checks wheter there is any tables (will not create new tables if it finds ANY table) if (!tables.next()) { new ResourceDatabasePopulator(new ClassPathResource("schema-javadb.sql"), new ClassPathResource("test-data.sql")).execute(ds); } } catch (SQLException ex) { System.out.println("SQL Ex when checking for tables"); System.out.println(ex.getMessage()); } return ds; }
From source file:de.tudarmstadt.ukp.wikipedia.util.DbUtilities.java
public boolean tableExists(String tableName) { try {/*from w ww.j av a2s .co m*/ DatabaseMetaData dbmd = conn.getMetaData(); // Specify the type of object; in this case we want tables String[] types = { "TABLE" }; // get all table names ResultSet resultSet = dbmd.getTables(null, null, "%", types); while (resultSet.next()) { if (resultSet.getString("TABLE_NAME").equals(tableName)) { return true; } } } catch (SQLException e) { logger.error("Table " + tableName + " does not exist.", new Throwable()); } return false; }
From source file:JDBCUtil.java
/** * Checks database metadata to see if a table exists. This method * is sensitive to the case of the provided table name. * * @param dbMetaData the database metadata to be used to look up this table * @param tableName the case sensitive table name * * @throws SQLException if an exception is encountered while accessing the database *///from ww w . ja v a 2 s.c o m public boolean tableExistsCaseSensitive(DatabaseMetaData dbMetaData, String tableName) throws SQLException { ResultSet rsTables = dbMetaData.getTables(null, null, tableName, null); try { boolean found = rsTables.next(); return found; } finally { closeJDBCResultSet(rsTables); } }
From source file:ws.rocket.sqlstore.test.SqlStoreTest.java
@BeforeClass public void setUp() throws SQLException, IOException { ResourceBundle bundle = ResourceBundle.getBundle("ws.rocket.sqlstore.test.test"); File dbPath = new File(bundle.getString("jdbc.dbPath")); if (!dbPath.exists()) { dbPath.mkdirs();/*from ww w . jav a2 s. co m*/ } System.setProperty("derby.system.home", dbPath.getCanonicalPath()); BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(bundle.getString("jdbc.driver")); ds.setUrl(bundle.getString("jdbc.url")); ds.setDefaultReadOnly(true); ds.setDefaultAutoCommit(false); ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); ds.setInitialSize(Integer.parseInt(bundle.getString("jdbc.initialConnections"))); this.dataSource = ds; boolean tableExists; try (Connection c = this.dataSource.getConnection()) { DatabaseMetaData meta = c.getMetaData(); try (ResultSet rs = meta.getTables(null, "SQLSTORE", "PERSON", new String[] { "TABLE" })) { tableExists = rs.next(); } } if (tableExists) { dropTables(); } }