Search in sources :

Example 41 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project GameCore by Warvale.

the class SQLUtil method tableExists.

public static boolean tableExists(SQLConnection connection, String table) throws ClassNotFoundException, SQLException {
    checkAndOpen(connection);
    Connection c = connection.getConnection();
    DatabaseMetaData metaData = c.getMetaData();
    ResultSet rs = metaData.getTables(null, null, table, null);
    if (rs.next()) {
        return rs.getRow() == 1;
    }
    return false;
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 42 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project ats-framework by Axway.

the class MysqlEnvironmentHandler method checkDriverVersion.

private boolean checkDriverVersion(MysqlDbProvider dbProvider) {
    try {
        Connection connection = dbProvider.getConnection();
        DatabaseMetaData dmd = connection.getMetaData();
        int majorVersion = dmd.getDriverMajorVersion();
        int minorVersion = dmd.getDriverMinorVersion();
        log.info(new StringBuilder().append("JDBC driver used is : ").append(majorVersion).append(".").append(minorVersion).toString());
        // The older specification is used in drivers prior to 5.0 including 5.0
        if (majorVersion < 5) {
            return false;
        } else if (majorVersion == 5 && minorVersion == 0) {
            return false;
        }
    } catch (SQLException e) {
        log.error("Unable to determine driver version, falling back to JDBC3 specs", e);
        return false;
    } catch (DbException e) {
        log.error("Unable to determine driver version, falling back to JDBC3 specs", e);
        return false;
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 43 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project ats-framework by Axway.

the class AbstractDbProvider method getTableDescriptions.

/**
     * @return description about all tables present in the database
     */
@Override
public List<TableDescription> getTableDescriptions() {
    ResultSet tablesResultSet = null;
    List<TableDescription> tables = new ArrayList<TableDescription>();
    try (Connection connection = ConnectionPool.getConnection(dbConnection)) {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        tablesResultSet = databaseMetaData.getTables(null, null, null, new String[] { "TABLE" });
        while (tablesResultSet.next()) {
            if (this instanceof OracleDbProvider && !tablesResultSet.getString(2).equalsIgnoreCase(dbConnection.user)) {
                // Oracle gives us all tables from all databases, we filter here only ours
                continue;
            }
            String tableName = tablesResultSet.getString("TABLE_NAME");
            if (!isTableAccepted(tablesResultSet, dbConnection.db, tableName)) {
                // Table is skipped
                continue;
            }
            log.debug("Extracting description about '" + tableName + "' table");
            TableDescription table = new TableDescription();
            table.setName(tableName);
            table.setSchema(tablesResultSet.getString("TABLE_SCHEM"));
            table.setPrimaryKeyColumn(exctractPrimaryKeyColumn(tableName, databaseMetaData));
            table.setIndexes(extractTableIndexes(tableName, databaseMetaData, connection.getCatalog()));
            List<String> columnDescriptions = new ArrayList<>();
            extractTableColumns(tableName, databaseMetaData, columnDescriptions);
            table.setColumnDescriptions(columnDescriptions);
            tables.add(table);
        }
    } catch (SQLException sqle) {
        throw new DbException("Error extracting DB schema information", sqle);
    } finally {
        if (tablesResultSet != null) {
            try {
                tablesResultSet.close();
            } catch (SQLException e) {
                log.warn("Result set resouce could not be closed!", e);
            }
        }
    }
    return tables;
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 44 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project opennms by OpenNMS.

the class JDBCDetectorIT method setUp.

@Before
public void setUp() throws UnknownHostException, SQLException {
    MockLogAppender.setupLogging();
    m_detector = m_detectorFactory.createDetector();
    String url = null;
    String username = null;
    Connection conn = null;
    try {
        conn = m_dataSource.getConnection();
        DatabaseMetaData metaData = conn.getMetaData();
        url = metaData.getURL();
        username = metaData.getUserName();
        conn.close();
    } catch (final SQLException e) {
        e.printStackTrace();
        if (conn != null) {
            conn.close();
        }
    }
    m_detector.setDbDriver("org.postgresql.Driver");
    m_detector.setPort(5432);
    m_detector.setUrl(url);
    m_detector.setUser(username);
    m_detector.setPassword("");
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) Before(org.junit.Before)

Example 45 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project opennms by OpenNMS.

the class JdbcQueryDetectorIT method setUp.

@Before
public void setUp() throws SQLException {
    MockLogAppender.setupLogging();
    m_detector = m_detectorFactory.createDetector();
    String url = null;
    String username = null;
    Connection conn = null;
    try {
        conn = m_dataSource.getConnection();
        DatabaseMetaData metaData = conn.getMetaData();
        url = metaData.getURL();
        username = metaData.getUserName();
        conn.close();
    } catch (final SQLException e) {
        e.printStackTrace();
        if (conn != null) {
            conn.close();
        }
    }
    m_detector.setDbDriver("org.postgresql.Driver");
    m_detector.setPort(5432);
    m_detector.setUrl(url);
    m_detector.setUser(username);
    m_detector.setPassword("");
    m_detector.setSqlQuery("select count(nodeid) from node");
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) Before(org.junit.Before)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)361 ResultSet (java.sql.ResultSet)216 Connection (java.sql.Connection)169 SQLException (java.sql.SQLException)155 Test (org.junit.Test)87 Statement (java.sql.Statement)49 ResultSetMetaData (java.sql.ResultSetMetaData)42 ArrayList (java.util.ArrayList)40 PreparedStatement (java.sql.PreparedStatement)38 IOException (java.io.IOException)26 Properties (java.util.Properties)25 HashSet (java.util.HashSet)16 PhoenixDatabaseMetaData (org.apache.phoenix.jdbc.PhoenixDatabaseMetaData)16 HashMap (java.util.HashMap)15 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 PrintWriter (java.io.PrintWriter)10 Savepoint (java.sql.Savepoint)10 List (java.util.List)10 Map (java.util.Map)9 DataSource (javax.sql.DataSource)9