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:net.solarnetwork.node.dao.jdbc.derby.DerbyCustomFunctionsInitializer.java
private static void registerBitwiseFunctions(final Connection con, String schema) throws SQLException { DatabaseMetaData dbMeta = con.getMetaData(); ResultSet rs = dbMeta.getFunctions(null, null, null); Set<String> functionNames = new HashSet<String>(Arrays.asList(BITWISE_AND, BITWISE_OR)); while (rs.next()) { String schemaName = rs.getString(2); String functionName = rs.getString(3).toUpperCase(); if (schema.equalsIgnoreCase(schemaName) && functionNames.contains(functionName)) { functionNames.remove(functionName); }//from w w w .jav a 2 s .co m } // at this point, functionNames contains the functions we need to create if (functionNames.size() > 0) { final String sqlTemplate = "CREATE FUNCTION %s.%s( parm1 INTEGER, param2 INTEGER ) " + "RETURNS INTEGER LANGUAGE JAVA DETERMINISTIC PARAMETER STYLE JAVA NO SQL " + "EXTERNAL NAME 'net.solarnetwork.node.dao.jdbc.derby.ext.DerbyBitwiseFunctions.%s'"; if (functionNames.contains(BITWISE_AND)) { final String sql = String.format(sqlTemplate, schema, BITWISE_AND, "bitwiseAnd"); con.createStatement().execute(sql); } if (functionNames.contains(BITWISE_OR)) { final String sql = String.format(sqlTemplate, schema, BITWISE_OR, "bitwiseOr"); con.createStatement().execute(sql); } } }
From source file:Main.java
public static void getTables(Connection conn) throws Exception { String TABLE_NAME = "TABLE_NAME"; String TABLE_SCHEMA = "TABLE_SCHEM"; String[] TABLE_AND_VIEW_TYPES = { "TABLE", "VIEW" }; DatabaseMetaData dbmd = conn.getMetaData(); ResultSet tables = dbmd.getTables(null, null, null, TABLE_AND_VIEW_TYPES); while (tables.next()) { System.out.println(tables.getString(TABLE_NAME)); System.out.println(tables.getString(TABLE_SCHEMA)); }//from w w w. jav a 2s . c om }
From source file:com.paladin.sys.db.DBManager.java
/** * init DataSource by spring/*w w w.ja va2 s.c o m*/ */ private static final void initDataSourceFromXml(String _xmpPath, String _beanName) { try { ApplicationContext factory = new ClassPathXmlApplicationContext(new String[] { _xmpPath }); dataSource = (DataSource) factory.getBean(_beanName); Connection conn = getConnection(); DatabaseMetaData mdm = conn.getMetaData(); log.info("Connected to " + mdm.getDatabaseProductName() + " " + mdm.getDatabaseProductVersion()); closeConnection(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Student.java
public static void checkData() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", ""); Statement st = conn.createStatement(); ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (mrs.next()) { String tableName = mrs.getString(3); System.out.println("\n\n\n\nTable Name: "+ tableName); ResultSet rs = st.executeQuery("select * from " + tableName); ResultSetMetaData metadata = rs.getMetaData(); while (rs.next()) { System.out.println(" Row:"); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.println(" Column Name: "+ metadata.getColumnLabel(i + 1)+ ", "); System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": "); Object value = rs.getObject(i + 1); System.out.println(" Column Value: "+value+"\n"); }/*from ww w . j av a 2 s. c o m*/ } } }
From source file:com.qualogy.qafe.business.resource.rdb.query.enhancer.EnhancementManager.java
public static QueryContainer enhance(QueryContainer container, RDBDatasource dsResource) { if ((dsResource == null) || (dsResource.getDataSource() == null)) { throw new IllegalArgumentException( "Properties not read correctly or properties are incorrect, loading datasource failed"); }/*from w w w . j a v a 2 s . com*/ DataSource dataSource = dsResource.getDataSource(); Connection con = null; try { con = dataSource.getConnection(); DatabaseMetaData md = con.getMetaData(); for (Iterator<Query> iter = container.values().iterator(); iter.hasNext();) { Query query = (Query) iter.next(); if (query instanceof Batch) { for (Iterator<Query> iterator = ((Batch) query).getQueries().iterator(); iterator.hasNext();) { Query batchQuery = (Query) iterator.next(); batchQuery = enhance(batchQuery, container, md); } } else { query = enhance(query, container, md); } container.update(query); } } catch (SQLException e) { String error = e.getMessage() + "[ on source ]" + dsResource.toString(); throw new EnhancementFailedException(error); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { String error = e.getMessage() + "[ on source ]" + dsResource.toString(); throw new EnhancementFailedException(error); } } } return container; }
From source file:net.sf.jasperreports.engine.query.OracleProcedureCallHandler.java
protected static boolean isOracle(Connection connection) throws SQLException { String dbVendor = connection.getMetaData().getDatabaseProductName().toLowerCase(); return DB_PRODUCT.equals(dbVendor); }
From source file:jdbc.JdbcUtils.java
/** * Return whether the given JDBC driver supports JDBC 2.0 batch updates. * <p>//from www.j av a 2s . co m * Typically invoked right before execution of a given set of statements: to * decide whether the set of SQL statements should be executed through the * JDBC 2.0 batch mechanism or simply in a traditional one-by-one fashion. * <p> * Logs a warning if the "supportsBatchUpdates" methods throws an exception * and simply returns false in that case. * * @param con * the Connection to check * @return whether JDBC 2.0 batch updates are supported * @see java.sql.DatabaseMetaData#supportsBatchUpdates */ public static boolean supportsBatchUpdates(Connection con) { try { DatabaseMetaData dbmd = con.getMetaData(); if (dbmd != null) { if (dbmd.supportsBatchUpdates()) { if (logger.isDebugEnabled()) { logger.debug("JDBC driver supports batch updates"); } return true; } else { if (logger.isDebugEnabled()) { logger.debug("JDBC driver does not support batch updates"); } } } } catch (SQLException ex) { logger.warn("JDBC driver 'supportsBatchUpdates' method threw exception", ex); } catch (AbstractMethodError err) { logger.warn("JDBC driver does not support JDBC 2.0 'supportsBatchUpdates' method", err); } return false; }
From source file:org.owasp.webgoat.session.DatabaseUtilities.java
/** * <p>returnConnection.</p>/*from w w w .j av a2 s.co m*/ * * @param user a {@link java.lang.String} object. */ public static synchronized void returnConnection(String user) { try { Connection connection = connections.get(user); if (connection == null || connection.isClosed()) return; if (connection.getMetaData().getDatabaseProductName().toLowerCase().contains("oracle")) connection.close(); } catch (SQLException sqle) { sqle.printStackTrace(); } }
From source file:com.sap.dirigible.repository.db.dao.DBMapper.java
/** * ResultSet current row to Binary Content transformation * // w w w . ja v a 2s . co m * @param repository * @param resultSet * @return * @throws SQLException * @throws IOException */ public static byte[] dbToDataBinary(Connection connection, ResultSet resultSet, String columnName) throws SQLException, IOException { String productName = connection.getMetaData().getDatabaseProductName(); IDialectSpecifier dialectSpecifier = DBUtils.getDialectSpecifier(productName); InputStream is = dialectSpecifier.getBinaryStream(resultSet, columnName); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); byte[] bytes = baos.toByteArray(); return bytes; }
From source file:com.thoughtworks.go.server.database.DatabaseFixture.java
public static void assertColumnType(BasicDataSource dataSource, String tableName, String columnName, String expected) throws SQLException { Connection connection = dataSource.getConnection(); try {//from ww w .j a va 2 s .com ResultSet set = connection.getMetaData().getColumns(null, null, null, null); while (set.next()) { if (set.getString("TABLE_NAME").equalsIgnoreCase(tableName) && set.getString("COLUMN_NAME").equalsIgnoreCase(columnName)) { String typeName = set.getString("TYPE_NAME"); int typeWidth = set.getInt("COLUMN_SIZE"); String type = typeName + "(" + typeWidth + ")"; assertThat("Expected " + columnName + " to be " + expected + " type but was " + type, type, is(expected)); return; } } Assert.fail("Column " + columnName + " does not exist"); } finally { try { connection.close(); } catch (Exception ignored) { } } }