Example usage for java.sql Connection getMetaData

List of usage examples for java.sql Connection getMetaData

Introduction

In this page you can find the example usage for java.sql Connection getMetaData.

Prototype

DatabaseMetaData getMetaData() throws SQLException;

Source Link

Document

Retrieves a DatabaseMetaData object that contains metadata about the database to which this Connection object represents a connection.

Usage

From source file:com.clican.pluto.orm.tool.DatabaseMetadata.java

public DatabaseMetadata(Connection connection, Dialect dialect, boolean extras) throws SQLException {
    sqlExceptionConverter = dialect.buildSQLExceptionConverter();
    meta = connection.getMetaData();
    this.extras = extras;
    initSequences(connection, dialect);//from  w  ww  .ja va 2  s.  c  om
}

From source file:com.qubole.quark.plugins.jdbc.JdbcDB.java

protected ImmutableMap<String, Integer> getTypes(Connection connection) throws SQLException {
    DatabaseMetaData databaseMetaData = connection.getMetaData();
    ResultSet rs = databaseMetaData.getTypeInfo();
    ImmutableMap.Builder<String, Integer> builder = new ImmutableMap.Builder<>();
    while (rs.next()) {
        LOG.debug("Registering data type '" + rs.getString("TYPE_NAME") + "'");
        builder.put(rs.getString("TYPE_NAME").toUpperCase(), rs.getInt("DATA_TYPE"));
    }/*from ww  w .  j  av a2  s .  c  o  m*/

    return builder.build();
}

From source file:org.alfresco.hibernate.DialectFactoryBean.java

@SuppressWarnings("deprecation")
@Override/*from ww  w  .jav  a2s  . c o  m*/
public Dialect getObject() throws SQLException {
    Session session = ((SessionFactory) this.localSessionFactory.getObject()).openSession();
    Configuration cfg = this.localSessionFactory.getConfiguration();
    Connection con = null;
    try {
        // make sure that we AUTO-COMMIT
        con = session.connection();
        con.setAutoCommit(true);
        DatabaseMetaData meta = con.getMetaData();
        Dialect dialect = DialectFactory.buildDialect(cfg.getProperties(), meta.getDatabaseProductName(),
                meta.getDatabaseMajorVersion());
        dialect = changeDialect(cfg, dialect);
        return dialect;
    } finally {
        try {
            con.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.abixen.platform.module.chart.service.impl.AbstractDatabaseService.java

protected ResultSet getTablesAsResultSet(Connection connection) throws SQLException {
    DatabaseMetaData md = connection.getMetaData();
    return md.getTables(null, null, "%", null);
}

From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

private void runTwoSqlStatementsWithTwoConnections(PoolingDataSource poolingDataSource)
        throws SQLException, InterruptedException {

    Connection connection = poolingDataSource.getConnection();
    if (connection.getMetaData().getDatabaseProductName().toLowerCase().contains("mysql")) {
        assertNotNull(connection);/*w  ww . j a va2s  .  c  om*/

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select current_date()");

        statement = connection.createStatement();
        statement.execute("set session wait_timeout=1");
        assertTrue(resultSet.next());

        connection.close(); // return to pool

        // Wait for 2 seconds
        System.err.print("Sleeping for 2 seconds....");
        Thread.sleep(2 * 1000L);
        System.err.println("Running again now");
        connection = poolingDataSource.getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery("select current_time()");
    }
}

From source file:com.p6spy.engine.spy.MultipleDataSourceTest.java

private void validateSpyEnabled(DataSource ds) throws SQLException {
    assertNotNull("JNDI data source not found", ds);

    // get the connection
    Connection con = ds.getConnection();

    // verify that the connection class is a proxy
    assertTrue("Connection is not a proxy", ProxyFactory.isProxy(con.getClass()));

    if (con.getMetaData().getDatabaseProductName().contains("HSQL")) {
        con.createStatement().execute("set database sql syntax ora true");
    }//  w  w w  .  j  a v  a  2  s .  c o  m
    con.createStatement().execute("select current_date from dual");
    assertTrue(((P6TestLogger) P6LogQuery.getLogger()).getLastEntry().indexOf("select current_date") != -1);
}

From source file:com.sample.team5.WhatsYourTechController.java

public void createTablePlayers() throws SQLException {
    Connection conn;

    conn = dataSource.getConnection();/*from  www.ja  va2 s  .c om*/

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet res = meta.getTables(null, null, "players", null);
    if (res.next()) {
        System.out.println("Table 'players' already exists.");
        logger.info("Table 'players' already exists.");
    } else {
        Statement stmt = conn.createStatement();

        String query = "CREATE TABLE IF NOT EXISTS players (  " + "game_id int(11) NOT NULL AUTO_INCREMENT, "
                + "player varchar(45) DEFAULT NULL, " + "total_score int(11) NOT NULL DEFAULT 0, "
                + "PRIMARY KEY (game_id), " + "UNIQUE KEY game_id_UNIQUE (game_id), "
                + "KEY player_fk_idx (player), "
                + "CONSTRAINT player_fk FOREIGN KEY (player) REFERENCES users (username) ON DELETE NO ACTION ON UPDATE NO ACTION )";
        stmt.executeUpdate(query);
        logger.info("Table 'players' created.");
        System.out.println("Table 'players' created.");
    }
    conn.close();
}

From source file:com.sample.team5.WhatsYourTechController.java

public void createTableHints() throws SQLException {
    Connection conn;

    conn = dataSource.getConnection();/*  w w w . jav  a 2s.c o m*/

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet res = meta.getTables(null, null, "hints", null);
    if (res.next()) {
        System.out.println("Table 'hints' already exists.");
        logger.info("Table 'hints' already exists.");
    } else {
        Statement stmt = conn.createStatement();

        String query = " CREATE TABLE IF NOT EXISTS hints (  " + "hints_id int(11) NOT NULL AUTO_INCREMENT, "
                + "answer varchar(45) DEFAULT NULL, " + "hint1 varchar(300) DEFAULT NULL, "
                + "hint2 varchar(300) DEFAULT NULL, " + "hint3 varchar(300) DEFAULT NULL, "
                + "difficulty varchar(10) DEFAULT NULL, " + "PRIMARY KEY (hints_id), "
                + "UNIQUE KEY hints_id_UNIQUE (hints_id) )";
        stmt.executeUpdate(query);
        logger.info("Table 'hints' created.");
        System.out.println("Table 'hints' created.");
        populateTable();
    }
    conn.close();
}

From source file:net.solarnetwork.node.dao.jdbc.derby.DerbyOnlineSyncJob.java

private String getDbPath() {
    String dbPath = jdbcOperations.execute(new ConnectionCallback<String>() {
        @Override//from   w w  w  . j a va 2s  .c o m
        public String doInConnection(Connection con) throws SQLException, DataAccessException {
            DatabaseMetaData meta = con.getMetaData();
            String url = meta.getURL();
            Pattern pat = Pattern.compile("^jdbc:derby:(\\w+)", Pattern.CASE_INSENSITIVE);
            Matcher m = pat.matcher(url);
            String dbName;
            if (m.find()) {
                dbName = m.group(1);
            } else {
                log.warn("Unable to find Derby database name in connection URL: {}", url);
                return null;
            }

            String home = System.getProperty("derby.system.home", "");
            File f = new File(home, dbName);
            return f.getPath();
        }
    });
    return dbPath;
}