List of usage examples for java.sql Connection getMetaData
DatabaseMetaData getMetaData() throws SQLException;
DatabaseMetaData
object that contains metadata about the database to which this Connection
object represents a connection. From source file:com.predic8.membrane.core.interceptor.statistics.StatisticsJDBCInterceptor.java
@Override public void init() { if (dataSourceBeanId != DATASOURCE_BEAN_ID_ATTRIBUTE_CANNOT_BE_USED) dataSource = applicationContext.getBean(dataSourceBeanId, DataSource.class); Connection con = null; try {//from w w w. j a v a 2 s .c o m con = dataSource.getConnection(); idGenerated = JDBCUtil.isIdGenerated(con.getMetaData()); statString = JDBCUtil.getPreparedInsertStatement(idGenerated); logDatabaseMetaData(con.getMetaData()); createTableIfNecessary(con); } catch (Exception e) { throw new RuntimeException("Init for StatisticsJDBCInterceptor failed: " + e.getMessage()); } finally { closeConnection(con); } }
From source file:com.amediamanager.config.DatabaseSchemaResource.java
private Boolean doesDataSourceExist(final String tableName) throws Exception { boolean dataSourceExists = false; Connection connection = null; ResultSet results = null;// w ww. ja v a 2 s . c o m DatabaseMetaData metadata; try { connection = dataSource.getConnection(); metadata = connection.getMetaData(); results = metadata.getTables(null, null, tableName, null); dataSourceExists = results.next(); } catch (Exception e) { LOG.error("Failed to check datasource.", e); throw e; } finally { try { results.close(); connection.close(); } catch (Exception x) { } } return dataSourceExists; }
From source file:com.adito.jdbc.DBDumper.java
/** * Dump table creation SQL. It is up to the caller to close the stream and connections when * finished with.//from www.ja va2s . c o m * * @param writer write SQL to this writer. * @param conx connection to get data from * @param quoteChar character to use to quote strings * @param tables array of table names or <code>null</code> to dump all in * database * @throws Exception on any error */ public void dumpData(PrintWriter writer, JDBCConnectionImpl conx, char quoteChar, String[] tables) throws Exception { Connection jdbcConnection = conx.getConnection(); DatabaseMetaData dbMetaData = jdbcConnection.getMetaData(); if (tables == null) { ResultSet rs = dbMetaData.getTables(null, null, null, null); try { while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); String tableType = rs.getString("TABLE_TYPE"); if (tableType.equalsIgnoreCase("TABLE")) { dumpData(writer, conx, quoteChar, new String[] { tableName }); } } } finally { rs.close(); } } else { for (int i = 0; i < tables.length; i++) { String tableName = tables[i]; log.info("Dumping data for table " + tableName); // Data PreparedStatement stmt = jdbcConnection.prepareStatement("SELECT * FROM " + tableName); try { ResultSet rs2 = stmt.executeQuery(); try { while (rs2.next()) { dumpRow(writer, rs2); } } finally { rs2.close(); } } finally { stmt.close(); } writer.println(); } } }
From source file:broadwick.data.readers.DataFileReader.java
/** * Execute a command to create a table./*from ww w. j av a 2 s. c o m*/ * @param tableName the name of the table to be created. * @param createTableCommand the command to create the table. * @param connection the database connection to use to create the table. * @throws SQLException if a SQL error has been encountered. */ protected final void createTable(final String tableName, final String createTableCommand, final Connection connection) throws SQLException { final DatabaseMetaData dbm = connection.getMetaData(); // First check if the table already exists, some databases do not support // CREATE TABLE ??? IF NOT EXISTS // so we have to look at the database schema try (ResultSet resultSet = dbm.getTables(null, null, "%", null)) { boolean tableExists = false; while (resultSet.next()) { if (tableName.equalsIgnoreCase(resultSet.getString("TABLE_NAME"))) { log.debug("Table {} already exists, ignoring", tableName); tableExists = true; } } if (!tableExists) { try (Statement stmt = connection.createStatement()) { final String[] commands = createTableCommand.split(";"); for (int i = 0; i < commands.length; i++) { log.trace("Creating table {}", commands[i]); stmt.execute(commands[i]); } } catch (SQLException sqle) { connection.rollback(); log.error("Error while creating the table '{}'. {}", createTableCommand, Throwables.getStackTraceAsString(sqle)); throw sqle; } } // } catch (Exception e) { // log.error("Could not create database {}", Throwables.getStackTraceAsString(e)); } connection.commit(); }
From source file:com.sap.dirigible.repository.ext.db.DBUtils.java
public String specifyDataType(Connection connection, String commonType) throws SQLException { String productName = connection.getMetaData().getDatabaseProductName(); IDialectSpecifier dialectSpecifier = getDialectSpecifier(productName); return dialectSpecifier.getSpecificType(commonType); }
From source file:com.idega.block.article.data.CategoryBugRemover.java
public boolean isBadTableExist(String tableName) throws SQLException { Connection conn = null; try {//w w w .java2 s . co m conn = SimpleQuerier.getConnection(); DatabaseMetaData meta = conn.getMetaData(); ResultSet columnsInfo = meta.getColumns(null, null, tableName, null); if (columnsInfo.next()) { return Boolean.TRUE; } else { return Boolean.FALSE; } } finally { if (conn != null) conn.close(); } }
From source file:com.idega.block.article.data.CategoryBugRemover.java
public boolean isBadColunmExist(String column) throws SQLException { Connection conn = null; try {/* ww w .j a va 2s. c o m*/ conn = SimpleQuerier.getConnection(); DatabaseMetaData meta = conn.getMetaData(); ResultSet columnsInfo = meta.getColumns(null, null, "IC_CATEGORY", null); while (columnsInfo.next()) { String columnName = columnsInfo.getString("COLUMN_NAME"); if (columnName.equalsIgnoreCase(column)) { return Boolean.TRUE; } } } finally { if (conn != null) conn.close(); } return Boolean.FALSE; }
From source file:net.solarnetwork.node.dao.jdbc.test.PreparedStatementCsvReaderTests.java
private void importData(final String tableName) { final Map<String, ColumnCsvMetaData> columnMetaData = new LinkedHashMap<String, ColumnCsvMetaData>(8); jdbcTemplate.execute(new ConnectionCallback<Object>() { @Override//from w w w . j a va2 s . c o m public Object doInConnection(Connection con) throws SQLException, DataAccessException { columnMetaData.putAll(JdbcUtils.columnCsvMetaDataForDatabaseMetaData(con.getMetaData(), tableName)); String sql = JdbcUtils.insertSqlForColumnCsvMetaData(tableName, columnMetaData); PreparedStatement ps = con.prepareStatement(sql); Reader in; PreparedStatementCsvReader reader = null; try { in = new InputStreamReader(getClass().getResourceAsStream("csv-data-01.csv"), "UTF-8"); reader = new PreparedStatementCsvReader(in, CsvPreference.STANDARD_PREFERENCE); String[] header = reader.getHeader(true); Map<String, Integer> csvColumns = JdbcUtils.csvColumnIndexMapping(header); CellProcessor[] cellProcessors = JdbcUtils.parsingCellProcessorsForCsvColumns(header, columnMetaData); while (reader.read(ps, csvColumns, cellProcessors, columnMetaData)) { Savepoint sp = con.setSavepoint(); try { ps.executeUpdate(); } catch (SQLException e) { DataAccessException dae = jdbcTemplate.getExceptionTranslator().translate("Load CSV", sql, e); if (dae instanceof DataIntegrityViolationException) { con.rollback(sp); } else { throw e; } } } } catch (IOException e) { throw new DataAccessResourceFailureException("CSV encoding error", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // ignore } } } return null; } }); }
From source file:net.risesoft.soa.asf.web.controller.SystemController.java
private List<SysInfo> getDBInfo() { List list = new ArrayList(); String group = "4. ??"; try {// w ww . ja v a 2 s .c o m Connection conn = this.basicDataSource.getConnection(); try { DatabaseMetaData dbmd = conn.getMetaData(); list.add(new SysInfo("DatabaseProductName", dbmd.getDatabaseProductName(), group)); list.add(new SysInfo("DatabaseProductVersion", dbmd.getDatabaseProductVersion(), group)); list.add(new SysInfo("DatabaseMajorVersion", Integer.valueOf(dbmd.getDatabaseMajorVersion()), group)); list.add(new SysInfo("DatabaseMinorVersion", Integer.valueOf(dbmd.getDatabaseMinorVersion()), group)); list.add(new SysInfo("DriverName", dbmd.getDriverName(), group)); list.add(new SysInfo("DriverVersion", dbmd.getDriverVersion(), group)); list.add(new SysInfo("DriverMajorVersion", Integer.valueOf(dbmd.getDriverMajorVersion()), group)); list.add(new SysInfo("DriverMinorVersion", Integer.valueOf(dbmd.getDriverMinorVersion()), group)); } finally { conn.close(); } group = "5. ?"; BasicDataSource bds = this.basicDataSource; list.add(new SysInfo("InitialSize", Integer.valueOf(bds.getInitialSize()), group)); list.add(new SysInfo("MaxActive", Integer.valueOf(bds.getMaxActive()), group)); list.add(new SysInfo("MaxIdle", Integer.valueOf(bds.getMaxIdle()), group)); list.add(new SysInfo("MinIdle", Integer.valueOf(bds.getMinIdle()), group)); list.add(new SysInfo("MaxWait", Long.valueOf(bds.getMaxWait()), group)); list.add(new SysInfo("NumActive", Integer.valueOf(bds.getNumActive()), group)); list.add(new SysInfo("NumIdle", Integer.valueOf(bds.getNumIdle()), group)); list.add(new SysInfo("DriverClass", bds.getDriverClassName(), group)); list.add(new SysInfo("URL", bds.getUrl(), group)); list.add(new SysInfo("Username", bds.getUsername(), group)); list.add(new SysInfo("Password", "******", group)); } catch (Exception ex) { log.warn("???: " + ex.getMessage(), ex); } return list; }
From source file:com.abixen.platform.service.businessintelligence.multivisualisation.application.service.database.AbstractDatabaseService.java
protected ResultSet getTablesAsResultSet(Connection connection) throws SQLException { DatabaseMetaData md = connection.getMetaData(); return md.getTables(null, null, "%", null); }